Angular 2 attribute directive input values are undefined and not set correctly
Input()
s aren't available in the constructor. They are set by change detection, and change detection is run after the component is instantiated. The lifecycle hooks ngOnChanges
(every update) and ngOnInit
(once after the first ngOnChanges
call) are called after change detection updated an input:
Change
constructor(private el: ElementRef){
this.setElement();
}
to
constructor(private el: ElementRef) {};
ngOnInit() {
this.setElement();
}
ngOnInit()
is called after the inputs are initialized.
Instead of
this.el.nativeElement.style.color = this.colorHex;
it's better to use
@HostBinding('style.color')
@Input() colorHex : string;
@HostBinding('style.font-family')
@Input() fontFamily : string;
@HostBinding('style.font-weight')
@Input() fontWeight : string;
@HostBinding('style.font-style')
@Input() fontStyle : string;
Actually I haven't tried myself to add @HostBinding()
and @Input()
on the same field, but I don't see why it wouldn't work.
D M
Updated on May 04, 2020Comments
-
D M about 4 years
I have following directive (TextElementDirective), that has 4 input variables colorHex, fontFamily, fontWeight, fontStyle. I want to set an element's color and style using this directive.
@Directive( { selector: "[text-element-map]", // host: { // '(mousemove)': "onMouseMove()" // } } ) export class TextElementDirective{ @Input() colorHex : string; @Input() fontFamily : string; @Input() fontWeight : string; @Input() fontStyle : string; constructor(private el: ElementRef){ this.setElement(); } setElement(){ if(this.colorHex){ this.el.nativeElement.style.color = this.colorHex; } if(this.fontFamily){ this.el.nativeElement.style.fontFamily = this.fontFamily; } if(this.fontWeight){ this.el.nativeElement.style.fontWeight = this.fontWeight; } if(this.fontStyle){ this.el.nativeElement.style.fontStyle = this.fontStyle || ""; } } onMouseMove(){ this.setElement(); } }
When I use this directive in another component, as an attribute, it doesn't set the element style and color. Even if you click the button, the element values are not set.
If I use a host (onmousemove) in the directive, it works fine. But I want to set the values during start up.
Any idea what am i missing?
Here is the test component that uses it.
@Component({ template: ` <h3>Test</h3> <div> <span>text-element-map: </span> <span class="text-content" text-element-map [colorHex]="colorHex" [fontFamily]="fontFamily" [fontWeight]="fontWeight" [fontStyle]="fontStyle">Change this text</span> <button (click)="setCurrentDesignElement()">Click</button> </div> `, directives:[TextElementDirective], changeDetection: ChangeDetectionStrategy.Default }) export class TestComponent{ @ViewChild(TextElementDirective) private childTextElement: TextElementDirective; colorHex: string = "#e2e2e2"; fontFamily: string = "Arial"; fontWeight: string = "normal"; fontStyle: string = "normal"; setCurrentDesignElement(){ this.color = { hex: "#B4B4B4", id: 5745, name: "Athletic Heather" }; this.font = { cssString: "Valera Round", weight: "normal", style: "italic" }; this.colorHex = "#B4B4B4"; this.fontFamily = "Valera Round"; this.fontWeight = "normal"; this.fontStyle = "italic"; //this.childTextElement.setElement(); } }
-
D M about 8 yearsIt works when the directive is initialized. But when I click the button to reset colorHex, fontFamily, etc.. it doesn't reset the text. How do I make sure that the setElement () is called when the model changes? Why is the change detection not taking over?
-
Günter Zöchbauer about 8 yearsThere are two ways: make
colorHex
, ... setters and getters or changengOnInit()
tongOnChanges(changes)
.ngOnChanges
is called every time input values change. With my suggestion above you shouldn't needsetElement()
at all. -
D M about 8 yearsYes. You are right. When I use HostBinding, I don't need setElement function. Thanks a lot.
-
Astravagrant about 8 yearsDoh! So obvious now. Thankyou.
-
Drammy about 6 yearsShouldn't the ctor be changed to 'constructor(private el: ElementRef) {}'?
-
Günter Zöchbauer about 6 years@Drammy thanks - fixed (mixed in a bit of Dart syntax ;-) )