Turn of lazy loading in ngSwitch
Asked Answered
A

1

5

I have this code:

<ng-container [ngSwitch]="currentTab">
  <div [@ngSwitch]="'show'" *ngSwitchCase="1"><app-sub-search></app-sub-search></div>
  <div [@ngSwitch]="'show'" *ngSwitchCase="2"><app-filters></app-filters></div>
  <div [@ngSwitch]="'show'" *ngSwitchCase="3"><app-map></app-map></div>
</ng-container>

app-map contains google map and evey time Im switching to this tab its loads from the beginig. How I can here switch lazy loading off, that map will be loaded one time?

Amoakuh answered 2/3, 2020 at 16:57 Comment(0)
M
7

You could use the hidden directive instead of *ngSwitchCase on components you want to eager load.

<ng-container [ngSwitch]="currentTab">
  <div *ngSwitchCase="1"><app-sub-search></app-sub-search></div>
  <div *ngSwitchCase="2"><app-filters></app-filters></div>
  <div [hidden]="currentTab !== 3"><app-map></app-map></div>
</ng-container>

Using hidden will allow an instance of <app-map> to be created on load, but will keep it hidden when required.

DEMO: https://stackblitz.com/edit/angular-vfnzja

Mucor answered 2/3, 2020 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.