简体   繁体   中英

How to detect if property changed in Angular 2?

I have simple class which looks like this:

export class MessagesPage  {

    @ViewChild(Content) content: Content;

    public message  = '';
    public messages = [];

    public state = 'general';
}

Is it possible inside the class to detect if state property is changed?

Make them getters and setters. There is no other way to get notified about changes:

export class MessagesPage  {

    @ViewChild(Content) content: Content;

    public message  = '';
    public messages = [];

    private _state = 'general';
    public get state() { return this._state; }
    public set state(value:string) { 
      this._state = value;
      console.log('state changed', this._state);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM