I'm feeling a bit lost.
Check out this straightforward directive:
@Directive({
selector: '[myDirective]'
})
export class MyDirective {
private textContent: string;
private isEnabled: boolean;
@Input() myDirective:string;
@Input('myText')
set myTextValue(val: string) {
this.textContent = val;
}
@Input('myEnabled')
set myEnabledState(val: boolean) {
this.isEnabled = val;
}
ngOnInit() {
console.log("myDirective attribute value: " + this.myDirective);
console.log("myText content: " + this.textContent);
console.log("myEnabled state: " + this.isEnabled);
}
}
If my HTML code looks like this:
<div [myDirective]="defaultMsg" [myEnabled]="true" [myText]="abc"></div>
The output will be:
myDirective attribute value: defaultMsg // perfect
myEnabled state: true // correct
myText content: undefined // Why?
Now if I REMOVE the [] from myText
:
<div [myDirective]="defaultMsg" [myEnabled]="true" myText="abc"></div>
The output will be:
myDirective attribute value: defaultMsg // perfect
myEnabled state: true // correct
myText content: abc // SUCCESS
I could also remove the []
from myEnabled
and it would still function. This is where my confusion lies - when should square brackets []
be used and when not? I believe they should always be present to avoid any uncertainty for the users of myDirective
. What do you think?