Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何聆听角度的属性?_Javascript_Angular_Properties - Fatal编程技术网

Javascript 如何聆听角度的属性?

Javascript 如何聆听角度的属性?,javascript,angular,properties,Javascript,Angular,Properties,我对这一点表示怀疑。如何侦听同一类中的属性值更改 import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent { name:string } 在您的案例中,处理属性更改的一个简

我对这一点表示怀疑。如何侦听同一类中的属性值更改

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

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

export class AppComponent  { 
  name:string
}

在您的案例中,处理属性更改的一个简单方法是为您的属性实现getter和setter

示例:

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

你想干什么。请解释您的问题当属性更改时,您可以使用typescript setter和getter来执行某些操作。我认为ngOnChanges仅适用于输入属性。