是否有可能只检测 angular2 中特定绑定(bind)属性的变化?
export class Test{
@Input() a;
@Input() b;
constructor() {
}
ngOnChanges(){
//I want that this only called when a changed
console.log(this.a);
}
}
最佳答案
正如 Eric 所说,ngOnChanges
方法将在您的 @Input
每次更新时被调用。
如果你只想检测 @Input
"a"的更新,你可以使用 setter:
export class SubComponent {
@Input()
set a(a:string) {
this._a = a;
console.log('a updated');
}
@Input()
b:any;
(...)
}
关于javascript - Angular 2- ngOnChanges 仅适用于特定绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36061565/