I have two child components included in my parent component. Depending on a selection I am rendering one of them. This looks as follows in my html (the ngIf):
<app-default-child *ngIf="selectedExperiment && selectedExperiment.projectType === 'default'" (newFormEvent)="onExperimentOptionsFormChange($event)"></app-default-child>
<app-scenario-child *ngIf="selectedExperiment && selectedExperiment.projectType === 'scenario'" (newFormEvent)="onExperimentOptionsFormChange($event)"></app-scenario-child>
Now I want to call a function of one of the components inside of my ngOnInit() but my ViewChild is undefined.
export class myComponent implements OnInit, OnDestroy {
@ViewChild(DefaultChildComponent) defaultChild:DefaultChildComponent
@ViewChild(ScenarioChildComponent) scenarioChild:ScenarioChildComponent
selectedExperiment: Experiment
ngOnInit(){
this.doSomething();
}
doSomething(){
this.selectedExperiment = exp //I am getting this in elsewhere
// Here I am getting the undefined now, no matter which child
if(this.selectedExperiment.projectType === ProjectType.Default){
this.defaultChild.doSomething();
} else if (this.selectedExperiment.projectType === ProjectType.Scenario) {
this.scenarioChild.doSomething();
}
}
}