Open In App

Angular PrimeNG TabView Lazy Loading

Last Updated : 25 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use TabView Lazy Loading in Angular PrimeNG.

Angular PrimeNG facilitates the TabView component that acts as a container, in order to group the content with the tab. The Lazy Loading is used to lazy load the TabView data. Lazy loading helps initial load performance by only initializing the active tab, inactive tabs are not initialized until they get selected. A TabPanel is specified as lazy when there is a ngTemplate with pTemplate=”content” in it.

Syntax:

<p-tabPanel header="...">
  <ng-template pTemplate="content">
    <p>...</p>
  </ng-template>
</p-tabPanel>

 

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG TabView Lazy Loading.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Lazy Loading</h4>
  
<p-tabView>
    <p-tabPanel header="Coding">
        <p>Code Here</p>
    </p-tabPanel>
    <p-tabPanel header="Web Technologies">
        <ng-template pTemplate="content">
            <p>Learn About Web Technologies</p>
        </ng-template>
    </p-tabPanel>
    <p-tabPanel header="Articles">
        <ng-template pTemplate="content">
            <p>Read some Tech Articles</p>
        </ng-template>
    </p-tabPanel>
</p-tabView>


  • app.component.ts

Javascript




import { Component, OnInit } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [],
})
export class AppComponent {
    constructor() { }
    ngOnInit() { }
}


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TabViewModule } from 'primeng/tabview';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TabViewModule,
        ButtonModule,
        RouterModule.forRoot([
            { path: '', component: AppComponent }
        ])
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }


Output:

 

Example 2: Below is another example demonstrating the use of the Angular PrimeNG TabView Lazy Loading, where we are using lazy loading on dynamic tabs.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Lazy Loading</h4>
<p-tabView [(activeIndex)]="index">
    <p-tabPanel [header]="item.header"
                *ngFor="let item of items; 
                        let i = index
                [selected]="i == 0">
        <ng-template pTemplate="content">
            {{ item.content }}
        </ng-template>
    </p-tabPanel>
</p-tabView>


  • app.component.ts

Javascript




import { Component, OnInit } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [],
})
export class AppComponent {
    constructor() { }
  
    ngOnInit() { }
    items = [
        { header: 'Coding', content: 'Lets Code' },
        {
            header: 'Web Technologies',
            content: 'Lets Learn About Web Technologies',
        },
        
            header: 'Articles'
            content: 'Lets Read some Tech Articles' },
    ];
}


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TabViewModule } from 'primeng/tabview';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TabViewModule,
        ButtonModule,
        RouterModule.forRoot([
            { path: '', component: AppComponent }
        ])
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }


Output:

 

Reference: https://primefaces.org/primeng/tabview



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads