自定义组件的Angular 5数据绑定未检测到引用更改
在父组件中,我从子组件绑定对象属性,如下所示:自定义组件的Angular 5数据绑定未检测到引用更改,angular,data-binding,Angular,Data Binding,在父组件中,我从子组件绑定对象属性,如下所示: <app-chid-comp [date]="resultObj.date"></app-child-comp> 假设在一段时间后再次调用main part changeValue。 这是子组件: @Component({ selector: 'app-child-comp', templateUrl: 'child.component.html', styleUrls: ['./child.sty
<app-chid-comp [date]="resultObj.date"></app-child-comp>
假设在一段时间后再次调用main part changeValue。
这是子组件:
@Component({
selector: 'app-child-comp',
templateUrl: 'child.component.html',
styleUrls: ['./child.style.scss']
})
export class ChildComponent {
public mydate: any;
constructor() {}
@Input()
set date(date: any) {
this.mydate = date;
}
get date() {
return this.mydate;
}
}
我的问题来了:如果angular渲染组件,但再也不会渲染,为什么要设置“日期”初始值?
我想我可以用JSON.parse(JSON.stringify(value))
并且一定要用this.changedetator.markForCheck()触发它。
然后,我将尝试将字符串属性从父组件绑定到子组件,并在一段时间后再次更改它。结果是我从未得到子组件中的更改。然后我使用
Object.assign({},value)
得到了这个更改。但是,如果我使用一个对象并尝试从该对象绑定属性,这种方法不起作用。injectChangeDetectorRef
并在分配组件后调用detectChanges()
,请检查我的解决方案并反馈给我,谢谢。谢谢。我实现并测试了您的解决方案,但它不起作用。在子组件中注入更改检测器,并在ngOnInit中调用detectChanges()方法。是否正确?不在ngOnInit()
函数中,请在数据绑定后使用此选项尝试此选项:
@Component({
selector: 'app-child-comp',
templateUrl: 'child.component.html',
styleUrls: ['./child.style.scss']
})
export class ChildComponent {
public mydate: any;
constructor() {}
@Input()
set date(date: any) {
this.mydate = date;
}
get date() {
return this.mydate;
}
}