Comments (4)
What you are observing here is the timing of ngAfterViewInit
. The view tree in this example looks like:
- (root)
- AppRoot
- Test(Hello)
- EmbeddedView(ngIf)
- Test(World)
- AppRoot
One of the less obvious aspects of ngAfterViewInit
is that it pertains to the host view of a component, not the view created for its template. Since parent views aren't considered initialized until all child views within them have been initialized, it's clear to see that Test(World)
will receive ngAfterViewInit
after the embedded view which hosts it is initialized, and then Test(Hello)
will receive ngAfterViewInit
only after the whole AppRoot
component view is initialized.
from angular.
What you are observing here is the timing of
ngAfterViewInit
. The view tree in this example looks like:
(root)
AppRoot
Test(Hello)
EmbeddedView(ngIf)
- Test(World)
One of the less obvious aspects of
ngAfterViewInit
is that it pertains to the host view of a component, not the view created for its template. Since parent views aren't considered initialized until all child views within them have been initialized, it's clear to see thatTest(World)
will receivengAfterViewInit
after the embedded view which hosts it is initialized, and thenTest(Hello)
will receivengAfterViewInit
only after the wholeAppRoot
component view is initialized.
Thanks for answer @alxhub. I tried also ngOnInit
and its same behaviour, what is with your explanation of lifecycle flow expectable, but is there any "lifecycle hook" which is resolved like "capture"?
from angular.
ngDoCheck
is executed before change detection of a child view, and thus will flow from parent to child.
However, it definitely feels like an anti-pattern to have logic which depends on the relative ordering of lifecycle hooks between components. There are almost certainly better solutions, but without knowing your specific use case it's hard to recommend a design.
from angular.
I was registering Component in service on creation and I need it in order represented in DOM.
But, You are right it is anti-pattern in some way (I was also surprised by behaviour of ngOnInit
).
I found better (hope proper) solution, by using @ContentChildren
in parent.
Thanks for help.
Edit: I think we can close.
from angular.
Related Issues (20)
- @alan-agius4 For the supported versions, is the mentioned issue fixed because they have mentioned that all versions above 1.3.0 would have this issue. If that's the case updating the version wouldn't help us. HOT 2
- CAST Issue (CVE-2024-21490) HOT 2
- after render hook test is flaky in the repository HOT 1
- Alias `afterRender` to `afterEachRender` HOT 2
- Form control values do not get set with trailing space, making email validator difficult to diagnose HOT 3
- zone.js throws TypeError when passing AbortController as options to addEventListener HOT 1
- NG5002: Allow referencing of expression results in `@else if` block (control flow) HOT 2
- Router deactivate failure results in page reload in computed mode HOT 1
- Build visualize not working as mentioned in Doc HOT 1
- Add observed property to OutputEmitterRef HOT 10
- Button doesn't display in component HOT 4
- Conditionals and content projection HOT 3
- Automatic migration for RouterTestingModule to provideRouter([]) or RouterModule HOT 2
- `zone.js` `eventCoalescing: true` causes `dragover` event to misbehave in Chrome HOT 4
- adev: Local packages not linked properly for development HOT 3
- Signal computed not refreshed HOT 1
- Using viewChild/viewChildren with private property should be an error HOT 2
- Router links in `@defer` blocks not having correct relative route HOT 5
- Calling `ComponentFixture#detectChanges()` or `autoDetectChanges()` should re-trigger change detection after flushing effects HOT 2
- zone.js breaks on iOS 16 , starting at v0.13.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular.