Angular 2- ngOnChanges only for a specific binding

Viewed 10719

Is there a possibility to detect changes only for a specific binding property in angular2?

 export class Test{
     @Input() a;
     @Input() b;
     constructor() {
         }

     ngOnChanges(){
         //I want that this only called when a changed
         console.log(this.a);
     }
}
4 Answers

How about using SimpleChanges?

You could try something like this:

ngOnChanges(changes: SimpleChanges): void {
    const aChange = changes.a;
    if((aChange.previousValue && aChange.currentValue) && (aChange.previousValue != aChange.currentValue)){
        console.log('a changed');
    }
}
ngOnChanges(changes: SimpleChanges): void {
    if ('a' in changes) {
        // do your thing
    }
}
Related