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.

ViewChild not working on Angular 8 + Ivy Renderer

See original GitHub issue

🐞 bug report

Is this a regression?

Until Angular 7.2.14 everything works properly. Using Angular 8, it works too, if Ivy Renderer is not enabled.

Description

Using Ivy Renderer, the properties with the decorator ViewChild are not properly populated. I checked documentation without luck, so I don’t know if am I doing something wrong or if it is a bug.

🔬 Minimal Reproduction

See this project: https://github.com/igorgiovannini/angular-8-ivy-broken-viewchild

🔥 Exception or Error

ERROR TypeError: Cannot read property 'nativeElement' of undefined
    at AppComponent.ngOnInit (app.component.ts:14)
    at callHook (core.js:4842)
    at callHooks (core.js:4806)
    at executeHooks (core.js:4757)
    at executePreOrderHooks (core.js:4729)
    at refreshDescendantViews (core.js:11593)
    at renderComponentOrTemplate (core.js:11976)
    at tickRootContext (core.js:13258)
    at detectChangesInRootView (core.js:13294)
    at RootViewRef.detectChanges (core.js:19856)

🌍 Your Environment

Angular Version:


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 8.0.0-rc.2
Node: 10.14.1
OS: darwin x64
Angular: 8.0.0-rc.2
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.800.0-rc.2
@angular-devkit/build-angular     0.800.0-rc.2
@angular-devkit/build-optimizer   0.800.0-rc.2
@angular-devkit/build-webpack     0.800.0-rc.2
@angular-devkit/core              8.0.0-rc.2
@angular-devkit/schematics        8.0.0-rc.2
@ngtools/webpack                  8.0.0-rc.2
@schematics/angular               8.0.0-rc.2
@schematics/update                0.800.0-rc.2
rxjs                              6.4.0
typescript                        3.4.5
webpack                           4.30.0

Issue Analytics

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

github_iconTop GitHub Comments

17reactions
karacommented, May 6, 2019

@igorgiovannini The reason that your example doesn’t work is that you’re trying to access the results of a ViewChild query before the view has completed initializing. This is why your code works in the ngAfterViewInit hook (after view inits) and not the ngOnInit hook (before view inits).

In the previous runtime (View Engine), queries would be resolved at different times depending on where the results were located in the template and whether you were requesting ViewChild or ViewChildren. This is why in the official Angular docs, we recommend checking for query results in ngAfterViewInit - they are only guaranteed to populate by then (though in your case, the query populated earlier).

In Ivy, we have made the timing consistent so that both ViewChild and ViewChildren queries resolve after the view inits by default, rather than depending on the location of results in the template. It’s a bit easier to reason about, though it means that you won’t find view query results in ngOnInit by default. If you need the results of a query in that hook, you can explicitly choose to label the query as “static”.

So for your use case, you can either mark the query static (as the V8 schematic would do):

@ViewChild('test', {static: true}) test: ElementRef;

… or move the logic to ngAfterViewInit (preferred).

We will definitely have more documentation surrounding this as we get closer to Angular 8 final! But as this is expected behavior, I’m going to close this for now. Thanks for filing the issue.

2reactions
ericmartinezrcommented, May 6, 2019

Can you see if this issue https://github.com/angular/angular/issues/28643 is related to yours?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 8 viewChild returns undefined
I had a similar problem where ViewChild component is undefined in onInit() method. This fixed the issue: // Ensure Change Detection runs ......
Read more >
ViewChild
Property decorator that configures a view query. The change detector looks for the first element or the directive matching the selector in the...
Read more >
Component Queries Metadata Appears To Be Broken ...
When Ivy is enabled, @Component() queries don't work - only @ViewChild() (and similar) decorators appear to work. However, if Ivy is ...
Read more >
Angular 9/8 DOM Queries: ViewChild and ViewChildren ...
After running our Angular application the hello component is rendered and becomes part of the DOM so we can query it just like...
Read more >
In-depth analysis of ViewChild, ElementRef and ...
ViewChild decorator is not available when the parent component starts rendering. It will not be available on the ngOnInit() lifecycle hook. It ...
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