-
Notifications
You must be signed in to change notification settings - Fork 25.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: ViewChild property is set before OnInit #21800
Comments
If I'm not wrong that changed long ago over here #5993
|
Hmmm, but there is one problem. This allows you to use component before its initialization. Especialy if you have some code in property setters which are |
I tend to agree with @kukjevov - the current behaviour seems to be side-effect of implementation choices, rather than designing API to user expectations. |
Static queries (ViewChild and ContentChild that are not inside embedded view) are initialized during view creation https://github.com/angular/angular/blob/master/packages/core/src/view/view.ts#L333-L336. Dynamic queries (VIewChildren, ContentChildren and ViewChild/ContentChild that are located inside embedded view) are calculated during change detection cycle. https://github.com/angular/angular/blob/master/packages/core/src/view/view.ts#L361 As far as I know this behavior was added as part of new view engine here 4e7752a |
Because of this its now possible to get weird behaviors like this: https://stackblitz.com/edit/dog-stuff The value of an input property is setted on onInit, but its not reflected on the view or in the model itself at the start of afterViewInit |
We are going to make timing of |
Was this one solved by 19afb79 ? It looks like it could be it. |
I am closing this as it has been at least partially solved by angular 8 and need of |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
Currently when you have
before i dont know in which version was
this.tree
null before first call ofngAfterViewInit
, now its not, which allows me to accessFancyTreeComponent
before it was properly initialized!Before i was able to check for
null
and knew that component was not initialized.Even documentation says
View queries are set before the ngAfterViewInit callback is called.
which does not deny that it can be initialized beforengOnInit
but in past it was exactly like that it wasnull
.Is it a not mentioned breaking change?
Expected behavior
ViewChild
,ViewChildren
,ContentChild
,ContentChildren
should be initialized/set only afterngAfterViewInit
, respectivelyngAfterContentInit
which are not.Minimal reproduction of the problem with instructions
Use code above.
What is the motivation / use case for changing the behavior?
Environment
The text was updated successfully, but these errors were encountered: