Skip to content

@ViewChild results are inconsistant during the OnInit lifecycle #19872

Closed

Description

@wizarrc

I'm submitting a...


[ ] 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

http://plnkr.co/edit/KxGysUMkHTdO3C09F7Zs?p=preview

Notice how during OnInit the values are different depending on where the reference is.

What is the motivation / use case for changing the behavior?

This adds to inconsistent behavior and leads to hard to find bugs. It's better to fail fast.

Actual issue where this could have been found more easily if it failed fast.
swimlane/ngx-datatable#1066

Environment


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:

Activity

  1. alexzuza commented on Oct 23, 2017

    @alexzuza
    Contributor

    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

  2. wizarrc commented on Oct 23, 2017

    @wizarrc
    Author

    @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.

  3. wizarrc commented on Oct 23, 2017

    @wizarrc
    Author

    @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.

  4. trotyl commented on Oct 24, 2017

    @trotyl
    Contributor

    Since the API is clear:

    View queries are set before the ngAfterViewInit callback is called.

    And in guide:

    First, you have to import references to the ViewChild decorator and the AfterViewInit lifecycle hook.

    It's only guaranteed that ngAfterViewInit hook are safe enough to use. The status in ngOnInit is an undefined behavior may change w/o further notice.

  5. jasonaden commented on Nov 10, 2017

    @jasonaden
    Contributor

    It appears this behaves as expected. If you still feel there is an issue, please provide further details in a new issue.

  6. himanshu-singh1995 commented on Jan 1, 2019

    @himanshu-singh1995

    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?

  7. dummdidumm commented on Jan 23, 2019

    @dummdidumm

    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".

  8. wizarrc commented on Feb 21, 2019

    @wizarrc
    Author

    Angular 8 Beta 5 will allow the user to specify the timing of when this query is satisified.
    #28810

  9. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    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.

  10. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions