mue*_*ich 7 javascript angular
是否有可能只检测angular2中特定绑定属性的更改?
export class Test{
@Input() a;
@Input() b;
constructor() {
}
ngOnChanges(){
//I want that this only called when a changed
console.log(this.a);
}
}
Run Code Online (Sandbox Code Playgroud)
Thi*_*ier 13
正如埃里克所说,这个ngOnChanges方法将在你@Input的每次更新时被调用.
如果您只想检测@Input"a" 的更新,可以使用setter:
export class SubComponent {
@Input()
set a(a:string) {
this._a = a;
console.log('a updated');
}
@Input()
b:any;
(...)
}
Run Code Online (Sandbox Code Playgroud)
请参阅此plunkr:https://plnkr.co/edit/UKyRiq ? p = preview .