Sa Hagin
Sa Hagin

Reputation: 532

Why is change detection not triggering on nested dynamic component

I have an issue with change detection on nested dynamic component using content projection. Change detection is not triggering automatically on the child component, I have to add manual change detection to every actions.

An exemple here : https://stackblitz.com/edit/angular-ivy-k2z661?file=src%2Fapp%2Fapp.component.ts

As you can see, if you click the button, nothing is showing, but when adding the this.cdr.detectChanges() line on the add() function, the content is showing. Also, you can notice that I'm not using ChangeDetectionStrategy.OnPush

Is there a way to achieve that without adding the manual change detection everywhere?

Upvotes: 0

Views: 1519

Answers (1)

Sa Hagin
Sa Hagin

Reputation: 532

So I finally found what was going on. The child view need to be manually attached to the appref. So

private appRef: ApplicationRef

need to be added to the constructor and then, attach the child view:

    this.appRef.attachView(refChild.hostView);

I've updated the Stackblitz.

Upvotes: 2

Related Questions