1

I have a component which is using OnPush change detection.
In this component I have a FormArray (inside of a FormGroup).
I have two buttons, which add(push) or remove(removeAt) FormGroup in FormArray.
I expect because of the OnPush change detection the changes are not visible (there is no change in the root FormGroup reference)
However the component is detecting the changes perfectly.

I find a similar example (StackBlitz) where the changeDetection called manually, but when I remove that part of the code, it is still working fine.

I checked the Angular source and my guess is that the updateValueAndValidity function call is doing the change detection, also this StackOverflow comment confirms it.

So can somebody explain how it is working exactly?
I wouldn't like to break my application in the future after an Angular update.

Thanks in advance!

1 Answers1

0

Even with OnPush strategy Angular automatically checks for changes in a component when an event handler in the component fires (like click event handler in your case). If you call removeAt or push not inside a click handler, but for instance in setTimeout, changes won't be visible.

Maxim Krabov
  • 685
  • 5
  • 17