I am getting ERROR DOMException [InvalidCharacterError: "String contains an invalid character"
code: 5
nsresult: 0x80530005
location: http://localhost:4200/vendor.bundle.js:67608]
when trying to compile an Angular component with a custom attribute directive. My code for the AppComponent, Directive and base template is given below:
leftAlign.directive.ts
import {
Directive,
HostListener,
HostBinding,
Input,
Output,
ElementRef,
Renderer2 // for host class binding
} from '@angular/core';
@Directive({
selector: '[leftAlign]'
})
export class LeftAlignDirective {
public constructor(
private el: ElementRef,
private renderer: Renderer2
) {
this.renderer.addClass(this.el.nativeElement, 'ui leftAlign');
}
}
app.component.ts
import { Component } from '@angular/core';
import { LeftAlignDirective } from './Directives/left-align.directive';
@Component({
selector: `app-root`,
templateUrl: './app.component.html'
})
export class AppComponent {
public static SemanticUiBaseDir = '../../semantic/dist/';
public getSemanticeUiBaseDir() : string{
return AppComponent.SemanticUiBaseDir;
}
public constructor() {}
}
app.component.html
!--
@ semantic-ui.min.css
-->
<link rel="stylesheet" type="text/css" href="/home/zerocool/km-live/FrontEndComponent/semantic/dist/semantic.css">
<!--
@ @semantic.min.js
-->
<script src="./home/zerocool/km-live/FrontEndComponent/semantic/semantic.js"></script>
<p leftAlign>This should be aligned left!</p>
I am interested in understanding the following: 1. What causes such errors? 2. What caused the error in this case?
Thanks
The problem is the space in addClass, it's not allowed.
I get a much more descriptive error when I try to do this
ERROR DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('ui leftAlign') contains HTML space characters, which are not valid in tokens.
You'll need to add the two classes individually.
this.renderer.addClass(this.el.nativeElement, 'ui');
this.renderer.addClass(this.el.nativeElement, 'leftAlign');
On a side note you should refer to AppComponent as this
from inside itself.
export class AppComponent {
public static SemanticUiBaseDir = '../../semantic/dist/';
public getSemanticeUiBaseDir() : string{
return this.SemanticUiBaseDir; // I changed AppComponent to this here
}
public constructor() {}
}