How can you know if a lazy loaded component has been rendered? I have the following HTML:
<mat-accordion>
<mat-expansion-panel>
<mat-expansion-panel-header>
Panel Summary
</mat-expansion-panel-header>
<ng-template matExpansionPanelContent>
<some-component></some-component>
</ng-template>
<mat-expansion-panel>
.... more panels with the same component
<mat-accordion>
By default the panels are closed so <some-component>
has not been rendered yet.
Then in the typescript I have the following code:
@Component({
selector: 'main-component',
templateUrl: './main-component.html',
style: ''
})
export class mainComponent implements OnInit {
@ViewChild(SomeComponent, {static: false})
someComponent: SomeComponent;
onButtonClick() {
this.someComponent.update();
}
Say the panels haven't been opened yet and the user clicks on the button I get the following error:
mainComponent.html: ERROR TypeError: Cannot read property 'update' of undefined
I know this is because the actual components are not loaded yet and we are trying to call its function. I want to keep lazy loading the panels to reduce the performance issues for having dynamic things in an angular materials expansion panel.
I can think of only one solution and its to not lazy load which isn't really an option. Any other solutions? Is there a way to know if ViewChild is undefined or is there a flag where viewChild will tell you if it found those components?