简体   繁体   English

通过更改数据绑定对象的属性来触发OnChanges

[英]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.

相关问题 OnChanges不会在部分对象更改时触发 - OnChanges does not trigger on partial object change 更新绑定对象的内容时不会触发 onChanges - The onChanges isn't fired when updating the content of a bound object 如果@Input是一个对象及其属性更新之一,则不会触发OnChanges - OnChanges doesnt fire if @Input is an object & one of its property updates LifecycleHook:OnChanges-不带@input的触发器 - LifecycleHook: OnChanges - trigger without @input 类错误地实现了接口“ OnChanges”。 类型中缺少属性“ ngOnChanges” - Class incorrectly implements interface 'OnChanges'. Property 'ngOnChanges' is missing in type 将属性从字符串更改为对象后,elasticsearch不再可以为数据对象编制索引 - After changing a property from a string to an object, elasticsearch can no longer index the data object 在 angular2 中,如何获取在为 @Input 发送的 object 上更改的属性的 onChanges - In angular2, how to get onChanges for properties changed on an object sent in for an @Input Angular2 - 更改属性 onClick 不会触发视图更新 - Angular2 - Changing property onClick doesn't trigger view update 为什么在* ngFor =“let x of xs”中更改数组中的元素顺序不会触发组件onChanges? - Why change of element order in an array in *ngFor=“let x of xs” wouldn't trigger component onChanges? 为什么在视图中调用函数会多次触发onChanges而不是prop变量— Angular - Why function call in view trigger onChanges multiple times but not prop variable — Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM