Mar*_*iyi 5 javascript angular
我在这个问题上看到了类似的问题,但没有答案对我有用.我有一个布尔值,只要异步任务完成就会改变,但奇怪的是,ngonchages不会随时更改.以下是我的代码:
import {
Component,
OnChanges,
SimpleChange
} from '@angular/core';
export class HomePage implements OnChanges {
isLoaded: boolean;
constructor() {
this.isLoaded = false;
this.loadData();
}
loadData() {
setTimeout(() => {
this.isLoaded = true;
console.log(this.isLoaded);
}, 3000);
}
ngOnChanges(changes) {
console.log("There has been a change ", changes); //this is not firing
}
}
Run Code Online (Sandbox Code Playgroud)
ngOnChanges@Input()是 Angulars 更改检测机制的生命周期回调,当Angulars 数据绑定更改时调用
当你有
@Input() isLoaded: boolean;
Run Code Online (Sandbox Code Playgroud)
和
<home-page [isLoaded]="someValue">
Run Code Online (Sandbox Code Playgroud)
并且someValue在父组件发生变化时,则进行变化检测更新isLoaded并调用ngOnChanges()。
对于您的情况,最简单的解决方案可能是使用 getter 而不是属性:
_isLoaded: boolean;
set isLoaded(value:bool) {
this._isLoaded = value;
this.doSomething(value)
}
get isLoaded() {
return this._isLoaded;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3264 次 |
| 最近记录: |