Detect change in a variable?

Renato Souza de Oliveira picture Renato Souza de Oliveira · Oct 17, 2016 · Viewed 22.1k times · Source

Is it possible to detect change in a variable?

I have the following:

@Input('name') name: string;

I would like to call a function whenever change is happened in this variable 'name'.

Is it possible?

Answer

Mathias picture Mathias · May 5, 2018

You can do it the following:

private _name = '';

@Input('name')
set name(name: string) {
   this._name = name;
   doSomeStuff();
}

get name(): string { return this._name; }