【发布时间】:2021-02-25 15:00:17
【问题描述】:
当我对对象的属性进行更改时,Angular 的默认更改检测不起作用:
@Component({
selector: 'parent-comp',
template: `<child-comp [mutableObject]="mutableObject"></child-comp>`
})
export class ParentComp implements OnInit {
mutableObject: MutableObject = { someProp: 'hello' };
onClickedSomething() {
this.mutableObject.someProp = 'goodbye';
}
}
@Component({
selector: 'child-comp',
template: `<h1>{{ mutableObject.someProp }}</h1>`
})
export class ChildComp implements OnInit, OnChanges {
@Input() mutableObject: MutableObject;
ngOnInit() {}
ngOnChanges(changes: SimpleChanges) {
console.log('change detected');
}
}
ngOnInit 在父子组件第一次加载时在子组件中被调用,但是当 onClickedSomething() 方法更改属性值时,子组件不会检测到更改。我确保没有使用 ChangeDetectionStrategy.OnPush。这里可能出了什么问题?
【问题讨论】:
标签: angular