简体   繁体   English

如何检测Angular 2中的属性是否更改?

[英]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? 类内部是否可以检测state属性是否已更改?

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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