I have three components, panel1, panel2, panel3. panel1 is always present. I want to hide panel2 until panel1 has been interacted with. This works fine because panel1 is always there.
The problem is when I want to hide/show panel3 until panel2 is interacted with. The problem the ngIf doesn't work because panel2 doesn't exist yet.
<div class="row">
<div class="col-md-8 col-centered">
<panel1 #p1 [answerKey]="survey.answerKey" (questionAnswered)="calculate($event)"></panel1>
</div>
</div>
<div class="row" *ngIf="p1.isPanelComplete()">
<div class="col-md-8 col-centered">
<panel2 #p2 [answerKey]="survey.answerKey" (questionAnswered)="calculate($event)"></panel2>
</div>
</div>
<div class="row" *ngIf="(p2.isPanelComplete())"> <!-- Problem Here -->
<div class="col-md-8 col-centered">
<panel3 #p3 [answerKey]="survey.answerKey" (questionAnswered)="calculate($event)"></panel3>
</div>
</div>
I tried to get around it with checking for both panel 1 and 2 like this
<div class="row" *ngIf="(p1.isPanelComplete() && p2.isPanelComplete())"> <!-- Problem Here -->
<div class="col-md-8 col-centered">
<panel3 #p3 [answerKey]="survey.answerKey" (questionAnswered)="calculate($event)"></panel3>
</div>
</div>
but that doesn't seem to work either. I've read not to use [hidden] so I'm trying to avoid that. Any other ways to get around this?
Update for Gunter's Answer
Ok I created the directive
import {Output, EventEmitter, Directive, ElementRef, Renderer, Input} from '@angular/core';
@Directive({selector: 'onCreate'})
export class OnCreate {
@Output() onCreate = new EventEmitter();
ngOnInit() {
this.onCreate.emit('dummy');
}
}
I had to change directive p2 to p2c because I still need to ref the method in the component from the #p2. I don't get an error but it doesn't seem to fire. Trying to debug.
<div class="row">
<div class="col-md-8 col-centered">
<panel1 #p1 [answerKey]="survey.answerKey" (questionAnswered)="calculate($event)"></panel1>
</div>
</div>
<div class="row" *ngIf="p1.isPanelComplete()">
<div class="col-md-8 col-centered">
<panel2 #p2 (onCreate)="p2c = true" [answerKey]="survey.answerKey" (questionAnswered)="calculate($event)"></panel2>
</div>
</div>
<div class="row" *ngIf="p2c && p2.isPanelComplete()">
<div class="col-md-8 col-centered">
<panel3 #p3 [answerKey]="survey.answerKey" (questionAnswered)="calculate($event)"></panel3>
</div>
</div>