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仅适用于输入属性。