question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

template.createEmbeddedView will never run change detection

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

No

Description

When a template is created via template.createEmbeddedView(...), the contents will not be initialized and change detection will never run either. Every change detection would have to be called manually via view.detectChanges().

Any components inside created template will not fire the ngOnInit life cycle hook until the detectChanges is called.

Might be related to: https://github.com/angular/angular/issues/41501

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-aldx8q?file=src%2Fapp%2Fhello.component.ts

Please provide the exception or error you saw

The error is that no change detection is ran for any templates/components created this way.

Please provide the environment you discovered this bug in

Angular CLI: 12.1.3
Node: 14.16.0
Package Manager: npm 7.16.0
OS: win32 x64

Angular: 12.1.3
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, google-maps, language-service, material
... platform-browser, platform-browser-dynamic, router
... service-worker

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1201.3
@angular-devkit/build-angular   12.1.3
@angular-devkit/core            12.1.3
@angular-devkit/schematics      12.1.3
@schematics/angular             12.1.3
rxjs                            6.6.7
typescript                      4.3.5

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pkozlowski-opensourcecommented, Aug 4, 2021

I’m with @alxhub here - the act of creating an embedded view (TemplateRef.createEmbeddedView) corresponds to the “creation” part of a template. In this (one-time) phase we create DOM elements but don’t evaluate dynamic parts. In order to evaluate dynamic parts one needs to change-detect views.

You can totally create “standalone” views and attach those to a change detection tree “outside” of the DOM tree by attaching view to ApplicationRef.

It all might not be super-intuitive but it seems to behave as designed.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 4, 2021

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

createEmbeddedView creates view outside of changeDetection
I have a template and set of context to populate templates with. Done this a few times now but after an unknown change...
Read more >
Handling Observables with Structural Directives in Angular
Change detection means that Angular will update the template bindings with the values from your component's class instance.
Read more >
Angular Templates — From Start to Source | Unicorn Utterances
Learn how templates work in Angular. From the basics to being able to read Angular source code and write your own structural directives....
Read more >
Simplified Angular Change Detection | by Pankaj Parkar | ngconf
So when PostListComponent component retrieves a data form the ngOnInit hook, it doesn't run change detection from the root component ( AppComponent )....
Read more >
Rendering A TemplateRef As A Child Of The Body Element In ...
On its own, a TemplateRef has no rendered output. As such, the user won't see the projected-content until we explicitly render it using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found