Open In App

Angular PrimeNG Accordion Lazy Loading

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 know how to use the Accordion Lazy Loading in Angular PrimeNG.

The Accordion Component is used to display a section of custom content in tabs.

Lazy Loading: Inactive tabs are not initialized until they are selected, which improves initial load time by only initializing the active tab. By default, the contents of a lazy-loaded accordionTab are cached so that they are not recreated upon selection. The cache attribute on AccordionTab can be used to set this behavior. When a ngTemplate has pTemplate=”content” as part of it, an AccordionTab is designated as lazy.

 

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:

 

Steps to run the application: Run the below command to see the output

ng serve --save

Example 1: This is the basic example that illustrates how to use the Angular PrimeNG Accordion Lazy Loading.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Accordian Lazy Loading</h5>
  
<p-accordion>
    <p-accordionTab header="Geeks 1">
        Content without Lazy load.
    </p-accordionTab>
    <p-accordionTab header="Geek 2">
        <ng-template pTemplate="content">
            Content with Lazy loading.
        </ng-template>
    </p-accordionTab>
</p-accordion>


  • app.component.ts:

Javascript




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


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import { AccordionModule } from 'primeng/accordion';
import { ButtonModule } from 'primeng/button';
import { ToastModule } from 'primeng/toast';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        AccordionModule,
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
  
export class AppModule { }


Output:

 

Example 2: This is another example that illustrates how to use the Angular PrimeNG Accordion Lazy Loading using images as content and enabling multiple as true.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Accordian Lazy Loading</h5>
  
<p-accordion [multiple]="true">
    <p-accordionTab header=
    "Complete Interview Preparation">
        <img src=
            height="200" width="250" alt="gfg" />
    </p-accordionTab>
    <p-accordionTab header="System Design-Lazy Loading">
        <ng-template pTemplate="content">
            <img src=
            height="200" width="250" alt="gfg" />
        </ng-template>
    </p-accordionTab>
</p-accordion>


  • app.component.ts:

Javascript




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


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import { AccordionModule } from 'primeng/accordion';
import { ButtonModule } from 'primeng/button';
import { ToastModule } from 'primeng/toast';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        AccordionModule,
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
  
export class AppModule { }


Output:

 

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



Last Updated : 24 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads