You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@ViewChild results are inconsistant during the OnInit lifecycle #19872
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
OnInit lifecycle provides result for @ViewChild when not inside template
Expected behavior
OnInit lifecycle not provide result for @ViewChild when not inside template
Updated Due to feedback, I feel the expected behavior should be a more simplified behavior such as split up @ViewChild with @StaticViewChild for static queries or better tooling to error when trying to access query properties at inappropriate times such as dynamic queries during the ngOnInit lifecycle.
Minimal reproduction of the problem with instructions
Angular version: 4.4.6
Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [x] Edge version EdgeHTML 16.16299
For Tooling issues:
- Node version: XX
- Platform:
Others:
For me it is expected behavior because static queries are filled during view creation before ngOnInit hook is called.
In your case #template2 is a static query because it is not inside embedded view while #template1 is dynamic query since it is located inside structural directive *ngif
Staic query can be only ViewChild and ContentChild. VIewChildren/ContentChildren are always dynamic queries
Reacted by Trotyl Yu, Max Koretskyi, Pavel Gurecki and himanshu-singh1995
@alexzuza I was unaware of the difference, good point! It still doesn't solve the usability issue. Maybe some tooling that would provide an error when trying to access a dynamic query before the correct lifecycle hook.
@alexzuza Another thought would be for a future version of Angular, provide StaticViewChild, etc for static query optimizations. I guess I was a little too harsh on the expected behavior. I really would prefer a simpler behavior or better tooling that prevents the non-experts from falling into the pit of failure and instead fall into the pit of success.
For me it is expected behavior because static queries are filled during view creation before ngOnInit hook is called.
@alexzuza As this information doesn't seem to be documented anywhere in the docs, can we rely on this information and use result of static queries in ngOnInIt?
In this part of the official docs, they use a @ViewChild-variable inside ngOnInit, which makes me tend more towards "you can rely on this information".
Activity
alexzuza commentedon Oct 23, 2017
For me it is expected behavior because static queries are filled during view creation before
ngOnInithook is called.In your case
#template2is a static query because it is not inside embedded view while#template1is dynamic query since it is located inside structural directive*ngifStaic query can be only ViewChild and ContentChild. VIewChildren/ContentChildren are always dynamic queries
wizarrc commentedon Oct 23, 2017
@alexzuza I was unaware of the difference, good point! It still doesn't solve the usability issue. Maybe some tooling that would provide an error when trying to access a dynamic query before the correct lifecycle hook.
wizarrc commentedon Oct 23, 2017
@alexzuza Another thought would be for a future version of Angular, provide
StaticViewChild, etc for static query optimizations. I guess I was a little too harsh on the expected behavior. I really would prefer a simpler behavior or better tooling that prevents the non-experts from falling into the pit of failure and instead fall into the pit of success.trotyl commentedon Oct 24, 2017
Since the API is clear:
And in guide:
It's only guaranteed that
ngAfterViewInithook are safe enough to use. The status inngOnInitis an undefined behavior may change w/o further notice.jasonaden commentedon Nov 10, 2017
It appears this behaves as expected. If you still feel there is an issue, please provide further details in a new issue.
himanshu-singh1995 commentedon Jan 1, 2019
@alexzuza As this information doesn't seem to be documented anywhere in the docs, can we rely on this information and use result of static queries in ngOnInIt?
dummdidumm commentedon Jan 23, 2019
In this part of the official docs, they use a
@ViewChild-variable insidengOnInit, which makes me tend more towards "you can rely on this information".wizarrc commentedon Feb 21, 2019
Angular 8 Beta 5 will allow the user to specify the timing of when this query is satisified.
#28810
angular-automatic-lock-bot commentedon Sep 14, 2019
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.