Angular2 @输入到具有get/set的属性

前端之家收集整理的这篇文章主要介绍了Angular2 @输入到具有get/set的属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在该组件中有一个Angular2组件,它当前有一堆字段,它们之前应用@Input()来允许绑定到该属性,即
@Input() allowDay: boolean;

我想要做的是使用get / set绑定到一个属性,以便我可以在setter中做一些其他的逻辑,如下所示

_allowDay: boolean;
    get allowDay(): boolean {
        return this._allowDay;
    }
    set allowDay(value: boolean) {
        this._allowDay = value;
        this.updatePeriodTypes();
    }

在Angular2中怎么做?

基于Thierry Templier的建议,我将其改为,但是抛出错误无法绑定到“allowDay”,因为它不是已知的本机属性

//@Input() allowDay: boolean;
_allowDay: boolean;
get allowDay(): boolean {
    return this._allowDay;
}
@Input('allowDay') set allowDay(value: boolean) {
    this._allowDay = value;
    this.updatePeriodTypes();
}
您可以直接在setter上设置@Input,如下所述:
_allowDay: boolean;
get allowDay(): boolean {
    return this._allowDay;
}

@Input('allowDay')
set allowDay(value: boolean) {
    this._allowDay = value;
    this.updatePeriodTypes();
}

看到这个plunkr:https://plnkr.co/edit/6miSutgTe9sfEMCb8N4p?p=preview

原文链接:https://www.f2er.com/angularjs/144077.html

猜你在找的Angularjs相关文章