【问题标题】:Angular 2 attribute directive input values are undefined and not set correctlyAngular 2 属性指令输入值未定义且未正确设置
【发布时间】:2016-06-29 19:34:21
【问题描述】:

我有以下指令 (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();
}


}

【问题讨论】:

    标签: angular directive


    【解决方案1】:

    Input()s 在构造函数中不可用。它们由变更检测设置,变更检测在组件实例化后运行。生命周期挂钩 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(),但是我不明白为什么它不起作用。

    【讨论】:

    • 指令初始化时生效。但是当我单击按钮重置 colorHex、fontFamily 等时,它不会重置文本。如何确保在模型更改时调用 setElement()?为什么变更检测没有接管?
    • 有两种方法:制作colorHex, ... setter 和 getter 或将ngOnInit() 更改为ngOnChanges(changes)。每次输入值更改时都会调用ngOnChanges。根据我上面的建议,您根本不需要setElement()
    • 噢!现在太明显了。谢谢。
    • 不应该把ctor改成'constructor(private el: ElementRef) {}'吗?
    • @Drammy 感谢 - 已修复(混合了一些 Dart 语法;-))
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2017-08-26
    • 2018-12-28
    相关资源
    最近更新 更多