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.

ivy ViewChild in ng-container of ngSwitch

See original GitHub issue

🐞 bug report

Affected Package

@angular/core

Is this a regression?

Yes, the previous version 8 worked

Description

When using template ref in ngSwitch container in any life cycle of component ViewChild is always undefined

🔬 Minimal Reproduction

🔥 Exception or Error

ts:

@ViewChild('tip', { static: false })
public tip: NgbTooltip;

modeEnum: typeof GuiStatus = GuiStatus;
mode = GuiStatus.VIEW;

ngOnInit(): void {
    console.log('ngOnInit:tip', this.tip);    
}
ngOnChanges(_changes: SimpleChanges): void {
   console.log('ngOnChanges:tip', this.tip);
}

ngAfterViewInit(): void {
 console.log('ngAfterViewInit:tip', this.tip);
}

html:

<ng-container [ngSwitch]="mode">
  <ng-container *ngSwitchCase="modeEnum.VIEW">
     <i class="fas fa-info-circle text-primary"
             style="cursor:pointer"
             #tip="ngbTooltip"
             [ngbTooltip]="tipSource"
             tooltipClass="info-tooltip"></i>
  </ng-container>
</ng-container>

log:

ngOnChanges:tip undefined
ngOnInit:tip undefined
ngAfterViewInit:tip undefined

🌍 Your Environment

Angular Version:


Angular CLI: 9.0.6
Node: 12.6.0
OS: win32 x64

Angular: 9.0.6
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.900.6
@angular-devkit/build-angular     0.900.6
@angular-devkit/build-optimizer   0.900.6
@angular-devkit/build-webpack     0.900.6
@angular-devkit/core              9.0.6
@angular-devkit/schematics        9.0.6
@ngtools/webpack                  9.0.6
@schematics/angular               9.0.6
@schematics/update                0.900.6
rxjs                              6.5.4
typescript                        3.7.5
webpack                           4.41.2

Anything else relevant?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pkozlowski-opensourcecommented, Mar 20, 2020

@sysmat for me it behaves as designed / implemented - when the ngAfterViewInit fires the element queried for is not yet available. Only after some delay, and in the subsequent change detection the element queried for is added to the DOM so only then you can access it. I think that what you need is the ngAfterViewChecked lifecycle hook (and obviously you would have to check if query has results): https://ng-run.com/edit/INoBryc4MfnkkQ2Hapyh?open=app%2Fuser-source%2Fuser-source.component.ts

1reaction
pkozlowski-opensourcecommented, Mar 20, 2020

@JoostK your are totally right!!! This is my hasty copy & paste! This is why we need live reproduce scenarios!

Anyway, I’ve updated the repro to include querying both by a directive type and a local ref and indeed both seem to work as expected: https://ng-run.com/edit/WHoAaQmNxJLADbuqUwX8?open=app%2Fapp.component.ts

@sysmat could you please share the minimal reproduce scenario (you can modify the ng-run example I’ve provided) to expose the issue? As things stand right now I can’t reproduce so it is not actionable for us, I’m afraid…

Read more comments on GitHub >

github_iconTop Results From Across the Web

ViewChild within ngSwitch is undefined - Stack Overflow
I'm trying to access a template reference within some nested structural directives: <ng-container [ ...
Read more >
Understanding the ViewChild and ViewChildren decorators in ...
The @ViewChild and @ViewChildren decorators in Angular provide access to child elements in the view DOM by setting up view queries.
Read more >
Rendering A TemplateRef As A Child Of The Body Element In ...
It seems that the part <viewRef>.detectChanges(); renders the element and makes it available. It might be because im using ivy, because there ...
Read more >
ngSwitchCaseFinal - StackBlitz
<ng-container [ngSwitch]="grid">. <section class="grid1". *ngSwitchCase="'grid1'">. <div class="leftPanel">. <div *ngTemplateOutlet="thumbnail"></.
Read more >
ngTemplateOutlet | Angular 10 Tutorials in Hindi | Part-53
ng-container in angular | ngTemplateOutlet | Angular 10 Tutorials in Hindi | Part-53 ... IVY Compiler Vs View Engine | Rendering Architecture |...
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