Just to throw another alternative out there taken from this blog.
export class ChildComponent implements OnChanges {
/* METHOD THREE
* Using a decorator
*/
@OnChange<number>((value, simpleChange) => {
console.log('OnChange decorator methodThree', value)
})
@Input()
methodThree: number;
}
Decorator
export interface SimpleChange<T> {
firstChange: boolean;
previousValue: T;
currentValue: T;
isFirstChange: () => boolean;
}
export function OnChange<T = any>(callback: (value: T, simpleChange?: SimpleChange<T>) => void) {
const cachedValueKey = Symbol();
const isFirstChangeKey = Symbol();
return (target: any, key: PropertyKey) => {
Object.defineProperty(target, key, {
set: function (value) {
// change status of "isFirstChange"
if (this[isFirstChangeKey] === undefined) {
this[isFirstChangeKey] = true;
} else {
this[isFirstChangeKey] = false;
}
// No operation if new value is same as old value
if (!this[isFirstChangeKey] && this[cachedValueKey] === value) {
return;
}
const oldValue = this[cachedValueKey];
this[cachedValueKey] = value;
const simpleChange: SimpleChange<T> = {
firstChange: this[isFirstChangeKey],
previousValue: oldValue,
currentValue: this[cachedValueKey],
isFirstChange: () => this[isFirstChangeKey],
};
callback.call(this, this[cachedValueKey], simpleChange);
},
get: function () {
return this[cachedValueKey];
},
});
};
}
This method uses a typescript decorator. IMHO decorators are likely to stay but...
Decorator are an experimental feature that may change in future releases
myService()with us? the callback indicates that you are working with some asyncronous here, which would explain why your code does not work the way you intend