Detect change in a variable?

早过忘川 提交于 2020-05-12 14:48:26

问题


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?


回答1:


You can do it the following:

private _name = '';

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

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



回答2:


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

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



来源:https://stackoverflow.com/questions/40077714/detect-change-in-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!