Как прослушать недвижимость в Angular? - PullRequest
0 голосов
/ 11 мая 2019

У меня есть сомнения в угловых.Как прослушать изменения значения свойства в том же классе.

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})

export class AppComponent  { 
  name:string
}

1 Ответ

0 голосов
/ 11 мая 2019

Простой способ реагировать на изменение свойства в вашем случае - это использовать геттеры и сеттеры для вашей собственности.

Пример:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})

export class AppComponent  { 
  private _name: string

  set name(data) {
    this._name = data;

    // here you can trigger something on data change
    // do something when data is assigned to name
  }

  get name() {
    return this._name;
  }

  //another example for setters as @Input properties
  //here you will act if the component receives input change
  //from a parent component
  @Input
  set name(data: any) {
    this._name = data;

    // here you can trigger something on data change
    // do something when data is assigned to name
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...