Optimizing loading tab content
When tabs contain complex data or media-rich content, it’s beneficial to load the content of tabs lazily. By doing so, we aim to minimize initial page load times, conserve bandwidth, and ensure a smooth and responsive interface. So, let’s create a component like that.
How to do it…
In this recipe, we’ll have a simple tab group of two tabs. Only when a tab is selected will we lazy-load the component representing the contents of that tab. Each tab is represented in the URL, so whenever we change tabs, we’re navigating to a separate page.
Step 1 – Defining a tab group and an active tab
In our tabs.component.html
file, we’ll use the Angular Material tab to represent a tab group in the UI:
<mat-tab-group
[selectedIndex]="(activeTab$ | async)?.index"
(selectedTabChange)="selectTab($event)"
>
<ng-container *ngFor="let tab of tabs">
<mat-tab [label]="tab.label"></mat-tab>
</ng-container>
</mat-tab-group>
Now, inside tabs.component.ts
, we need to define the activeTab
and loading
states, as well as the content of a tab stream that we can subscribe to:
activeTab$ = new BehaviorSubject<TabConfig | null>(null);
activeTabContent$!: Observable<
typeof TabContentComponent |
typeof TabContent2Component |
null
>;
loadingTab$ = new BehaviorSubject<boolean>(false);
Now, we can hook into Angular Router events, filter events when navigation ends, and, based on an active URL, mark the corresponding tab as active:
this.router.events.pipe(
filter((event) => event instanceof NavigationEnd),
takeUntil(this.destroy$)
).subscribe({
next: () => {
const activeTab = this.tabs.find(
(tab) => tab.route === this.router.url.slice(1)
);
this.activeTab$.next(activeTab || null);
},
});
Step 2 – Loading tab content
Since we know which tab is active, we can start loading the content of that tab:
private loadTabContent(tab: TabConfig) {
const content$ = tab.route === 'tab1'
? of(TabContentComponent)
: of(TabContent2Component);
return content$.pipe(delay(1000));
}
this.activeTabContent$ = this.activeTab$.pipe(
tap(() => this.loadingTab$.next(true)),
switchMap((tab) =>
this.loadTabContent(tab!).pipe(
startWith(null),
catchError((error) => {
this.errors$.next(error);
return of(null);
}),
finalize(() => this.loadingTab$.next(false))
)
),
shareReplay({ bufferSize: 1, refCount: true })
);
Inside the loadTabContent
method, we’ll create an Observable out of the Angular component that’s matched based on the current route. Once we’ve done this, we’re ready to stream into the tab content whenever the active tab changes. We can do this by starting the loading state, switching to the stream that’s loading content, and resetting the loading state once the content has arrived.
Now, all we need to do is represent the content in the UI. Back in our tabs.component.html
file, we can simply add the following code:
@if (loadingTab$ | async) {
<p>Loading...</p>
}
<ng-container
*ngComponentOutlet="activeTabContent$ | async"
></ng-container>
Now, by going to our browser, we’ll see that the content of a tab will only be loaded when we click on that specific tab:


Figure 2.6: Loading tabs
See also
- The
of
function: https://rxjs.dev/api/index/function/of - The
startWith
operator: https://rxjs.dev/api/operators/startWith - Angular’s Router’
NavigationEnd
event: https://angular.dev/api/router/NavigationEnd - The Angular Material tab component: https://material.angular.io/components/tabs/overview