Angular 2 属性指令输入值未定义且设置不正确

标签 angular directive

我有以下指令 (TextElementDirective),它有 4 个输入变量 colorHex、fontFamily、fontWeight、fontStyle。我想使用此指令设置元素的颜色和样式。

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

当我在另一个组件中使用此指令作为属性时,它不会设置元素样式和颜色。即使单击按钮,也不会设置元素值。

如果我在指令中使用主机 (onmousemove),它工作正常。但我想在启动期间设置这些值。

知道我错过了什么吗?

这是使用它的测试组件。

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


}

最佳答案

Input() 在构造函数中不可用。它们由变更检测设置,变更检测在组件实例化后运行。生命周期 Hook ngOnChanges(每次更新)和 ngOnInit(在第一次 ngOnChanges 调用之后)在更改检测更新输入后被调用:

改变

constructor(private el: ElementRef){
    this.setElement();
}

constructor(private el: ElementRef) {};

ngOnInit() {
  this.setElement();
}

ngOnInit() 在输入初始化后调用。


代替

this.el.nativeElement.style.color = this.colorHex;

更好用

@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;

实际上我还没有尝试在同一字段上添加 @HostBinding()@Input(),但我不明白为什么它不会'不工作。

关于Angular 2 属性指令输入值未定义且设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35993030/

相关文章:

javascript - 将 array[i] 从 for 循环存储到返回 undefined 的新数组

forms - 输入类型编号 "only numeric value"验证

java - 使用 Apache Velocity 生成 C/C++ 语言文件

angular - 大写输入字段指令

javascript - AngularJS 自定义指令显示 {{obj}} 但不显示 {{obj.prop}}?

在 C 编译期间更改函数体

angular - 使用 mdIcon 时从控制台获取错误

css - 如何禁用 Angular Material 分页按钮的涟漪效果?

angular - 在 Angular 2 中解析 HTML 字符串并提取和更新值

AngularJS - 如何模块化指令+模板组合?