I am using Angular 2.
Right now I have two @input aa
and bb
. I want to do:
aa
changes, do something.Might be able to do it this way
ngOnChanges(changes: { [propName: string]: SimpleChange }) {
if( changes['aa'] && changes['aa'].previousValue != changes['aa'].currentValue ) {
// aa prop changed
}
if( changes['bb'] && changes['bb'].previousValue != changes['bb'].currentValue ) {
// bb prop changed
}
}
I'm surprised that the unchanged properties are defined, though. From the cookbook, I would expect that only changed properties would be defined.
https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child-on-changes
If that is too verbose, you could also try using the setter approach:
_aa: string;
_bb: string;
@Input() set aa(value: string) {
this._aa = value;
// do something on 'aa' change
}
@Input() set bb(value: string) {
this._bb = value;
// do something on 'bb' change
}
https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child-setter