Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect change in a variable?

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?

like image 709
Renato Souza de Oliveira Avatar asked Oct 17 '16 02:10

Renato Souza de Oliveira


2 Answers

I am solve this question using default Angular feature named OnChanges, very similar to OnInit.

https://stackoverflow.com/a/61720541/13514355

like image 154
mariane muniz Avatar answered Nov 16 '22 06:11

mariane muniz


You can do it the following:

private _name = '';

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

get name(): string { return this._name; }
like image 20
Mathias Avatar answered Nov 16 '22 07:11

Mathias