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();
}
}