如果数据绑定(bind)对象之一上的属性发生更改,而不必将该属性设置为新对象,我如何让 ngOnChanges() 触发。

// component
@Input() myObj: ObjType;
// component code...

这不会触发更改
// outside component
dataBoundObj.width = 1000;

这确实
dataBoundObj = new ObjType();

最佳答案

当你改变一个对象时,Angular 不会检测到变化。但是,它在检查当前组件时会触发 ngDoCheck。因此,您可以自己执行检查并从那里触发 ngOnChanges:

ngDoCheck() {
   if (this.o.width !== this.oldWidth) {
      this.ngOnChanges();
   }
}

关于angular - 通过更改数据绑定(bind)对象上的属性来触发 OnChanges,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44857183/

10-13 03:18
查看更多