Angular2 @Input 到带有 get/set 的属性 - Angular2 @Input to a property with get/set

10/14 01:56
阅读数 0

问题:

I have an Angular2 component in that component it currently has a bunch fields that have @Input() applied before them to allow binding to that property, ie我在该组件中有一个 Angular2 组件,它当前有一堆字段,在它们之前应用了 @Input() 以允许绑定到该属性,即

@Input() allowDay: boolean;

What I would like to do is actually bind to a property with get/set, so that I can do some other logic in the setter, something like the following我想要做的实际上是使用 get/set 绑定到一个属性,以便我可以在 setter 中执行一些其他逻辑,如下所示

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

how would I do this in Angular2?我将如何在 Angular2 中做到这一点?

Based on Thierry Templier suggestion I changed it to, but that throws the error Can't bind to 'allowDay' since it isn't a known native property :根据 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();
}

解决方案:

参考一: https://en.stackoom.com/question/2TnIM
参考二: https://stackoom.com/question/2TnIM
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部