我尝试检测变量子项的变化,但变量更改时ngOnChanges没有触发
@Input() _parent: any;
ngOnChanges(changes: {[propKey: string] : SimpleChange}) { << not fire
console.log('Change detected:');
setTimeout(() => {
if (!_.isEqual(this._parent.curScript, changes['_parent'].currentValue.curScript)) {
console.log(1);
}
}, 100);
}
我的问题是什么?
答案 0 :(得分:0)
如注释中所述,ngOnChanges仅检测输入属性的更改,这些更改在组件组合到另一个组件的模板时传入。生命周期钩子的Angular文档应该具有启发性https://angular.io/guide/lifecycle-hooks#onchanges
答案 1 :(得分:0)
。您可以通过ChangeDetectorRef检测更改。你可以像那样使用它
constructor( private dcr: ChangeDetectorRef) {
}
this.dcr.detectChanges(); // put this line where changes made for example you change `_parent` variable value from parent component you need to put that place. child value changed automatically. does this help