Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 attribute directive input values are undefined and not set correctly

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(); }   } 
like image 553
D M Avatar asked Mar 14 '16 16:03

D M


1 Answers

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.

like image 168
Günter Zöchbauer Avatar answered Sep 21 '22 05:09

Günter Zöchbauer