el_pup_le
el_pup_le

Reputation: 12189

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.

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

This doesn't trigger the change

// outside component
dataBoundObj.width = 1000;

This does

dataBoundObj = new ObjType();

Upvotes: 6

Views: 2451

Answers (1)

Max Koretskyi
Max Koretskyi

Reputation: 105517

Angular doesn't detect change when you mutate an object. However, it triggers ngDoCheck when checking current component. So you can perform a check yourself and trigger ngOnChanges from there:

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

Upvotes: 3

Related Questions