0

I am injecting a model reference from a parent component to a child component. When the attributes of that model reference are changed, I need to update the child component's view.

This cannot be done using ngOnChanges because the reference to the model is not changed. This is by design and is mentioned in the Angular docs.

A suggested alternative is using ngDoCheck. This would allow me to see if any of the model reference's attributes have changed and call functions accordingly. However, the Angular docs mention that this will be costly due to the frequency that this lifecycle hook will be called.

Right now, the child component is subscribed to an observable from a service that updates all of my model references. When the service updates a model, it notifies subscribers that it is time to update their views.

The issue here is if I have at least two child components with different model references. With the current design, each time any model is updated, all child components will update their view.

This seems wasteful, but I'm not sure if utilizing ngDoCheck will be any better.

Should I be using ngDoCheck to update child components individually, or should I stick to using an observable that forces all child components to update when any model is updated?

If it is the former, what is some good documentation for ngDoCheck? The internet seems to surprisingly lack information on this lifecycle hook.

  • How about you mark your component for check under subscription itself? – Ashish Ranjan Oct 25 '18 at 02:05
  • Okay, so the service is taking care of passing data from 'parent to child?' Even though you have Input props in child? What do you do in the subscription? Input props will anyway be updated(singleton objects), its just that ngOnChanges won't be called. You have some business logic in `ngOnChanges`? Like setting properties? If that so, Try moving the logic to `getters`. And which component of yours have onPush strategy? Parent? Child? – Ashish Ranjan Oct 25 '18 at 02:22

0 Answers0