2

I'm having problems using @ViewChild because it is nested within two different ngIf.

My template looks something like this:

<div *ngIf="model">
  <div *ngIf="otherModel">
    <myComponent #myComponent></myComponent>
  </div>
</div>
<div *ngIf="!model">
</div>

In template I use my ViewChild like:

    @ViewChild("myComponent", {static: false}) myComponent: MyComponent;

Now my problem is that my ViewChild always results as undefined. If I delete the first *ngIf="model" everything is working fine. But I guess I'm having trouble with two ngIfs.

Any suggestions?

Timo Löhr
  • 21
  • 1
  • This is a common problem when using `ngIf` in combination with `ViewChild`. You should be able to find a solution when searching for `ngIf ViewChild undefined`. For example here: https://stackoverflow.com/questions/39366981/viewchild-in-ngif – Nathan Feb 20 '22 at 23:48
  • I tried that solution with using a setter. But this only works when I'm using a single *ngIf. With two ngIfs my ViewChild results still as undefined. – Timo Löhr Feb 21 '22 at 07:20

0 Answers0