How to listen for value changes from class property TypeScript - Angular

前端 未结 5 1210
情话喂你
情话喂你 2020-12-13 06:57

In AngularJS, we can listen variable change using $watch, $digest... which is no longer possible with the new versions of Angular (5, 6).

I

5条回答
  •  我在风中等你
    2020-12-13 07:24

    as said you can use

    public set myProperty_2(value: type): void {
     if(value) {
      //doMyCheck
     }
    
     this._myProperty_2 = value;
    }
    

    and then if you need to retrieve it

    public get myProperty_2(): type {
      return this._myProperty_2;
    }
    

    in that way you can do all the checks that you want while setting/ getting your variables such this methods will fire every time you set/get the myProperty_2 property.

    small demo: https://stackblitz.com/edit/angular-n72qlu

提交回复
热议问题