[英]Trigger OnChanges by changing a property on the data bound object
How can I get ngOnChanges() to trigger if a property on one of the data bound objects changes rather than having to set the property to a new object. 如果某个数据绑定对象上的属性发生更改而不必将该属性设置为新对象,如何触发ngOnChanges()来触发。
// component
@Input() myObj: ObjType;
// component code...
This doesn't trigger the change 这不会触发更改
// outside component
dataBoundObj.width = 1000;
This does 这样做
dataBoundObj = new ObjType();
Angular doesn't detect change when you mutate an object. 当您改变对象时,Angular不会检测到更改。 However, it triggers
ngDoCheck
when checking current component. 但是,它会在检查当前组件时触发
ngDoCheck
。 So you can perform a check yourself and trigger ngOnChanges
from there: 所以你可以自己检查并从那里触发
ngOnChanges
:
ngDoCheck() {
if (this.o.width !== this.oldWidth) {
this.ngOnChanges();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.