Open In App

Angular PrimeNG Defer Component

Last Updated : 14 Nov, 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. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we are going to learn Angular PrimeNG Defer Component

The Defer component pauses or postpones the loading of content that is initially not in the viewport until it becomes visible on the scroll. This is done to check on data fetches and reduce unnecessary data access and also increase the speed of the site.

Syntax:

Import the module

import {DeferModule} from 'primeng/defer';

Apply the pDefer directive as follows:

<div pDefer>
    <ng-template>
       deferred content
   </ng-template>
</div>

Using the onLoad callback to perform some function

<div pDefer (onLoad)="initData()">
    <ng-template>
       deferred content
   </ng-template>
</div>

In the app.component.ts file,

initData(){
    // load data
}

Creating Angular application & Module Installation:

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

ng new geeks_angular

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

cd geeks_angular

Step 3: Install PrimeNG in your given directory.

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

Project Structure:

 

Example 1: In the following example, we have a deferred component that shows a toast on the scroll.

  • app.component.ts

HTML




<h1 style="color: green; text-align:center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG Defer Component</h3>
<div class="card" 
    style="height:800px;margin:auto;text-align:center">
      Scroll Down to show toast
</div>
<p-toast></p-toast>
<div pDefer (onLoad)="gfg()">
    <ng-template> </ng-template>
</div>


  • 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 {
    constructor(private messageService: MessageService) {}
    gfg() {
        this.messageService.add({
            severity: 'success',
            summary: 'GeeksforGeeks',
            detail: 'Angular PrimeNG Defer Component',
        });
    }
}


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import {CarService} from './carservice';
import {ToastModule} from 'primeng/toast';
import {DeferModule} from 'primeng/defer';
import {TableModule} from 'primeng/table';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        DeferModule,
        ToastModule,
        TableModule,
        HttpClientModule
    ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ],
    providers: [CarService]
})
  
export class AppModule { }


Output:

 

Example 2: In the following example, we load table data using the defer component.

  • app.component.html

HTML




<h1 style="color: green; text-align:center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG Defer Component</h3>
<div class="card" 
    style="height:800px;margin:auto;text-align:center">
      Scroll Down to show toast
</div>
<p-toast></p-toast>
<div pDefer (onLoad)="gfg()">
    <ng-template>
        <p-table [value]="tutorials" responsiveLayout="scroll">
            <ng-template pTemplate="header">
                <tr>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Rating</th>
                </tr>
            </ng-template>
            <ng-template pTemplate="body" let-tutorial>
                <tr>
                    <td>{{ tutorial.title }}</td>
                    <td>{{ tutorial.category }}</td>
                    <td>{{ tutorial.rating }}</td>
                </tr>
            </ng-template>
        </p-table>
    </ng-template>
</div>


  • 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 {
    constructor(private messageService: MessageService) {}
    tutorials: Tutorial[];
    gfg() {
        this.messageService.add({
            severity: 'success',
            summary: 'GeeksforGeeks',
            detail: 'Angular PrimeNG Defer Component',
        });
        this.loadData();
    }
    loadData() {
        this.tutorials = [
            {
              title: 'Queue',
              category: 'Data Structure',
              rating: 4,
            },
            {
              title: 'Circularly LinkedList',
              category: 'Data Structure',
              rating: 5,
            },
            {
              title: 'Doubly LinkedList',
              category: 'Data Structure',
              rating: 3,
            },
            {
              title: 'Singly LinkedList',
              category: 'Data Structure',
              rating: 4,
            },
            {
              title: 'Doubly Ended Queue',
              category: 'Data Structure',
              rating: 5,
            },
            {
              title: 'Binary Search Tree',
              category: 'Data Structure',
              rating: 4,
            },
            {
              title: 'Red Black Tree',
              category: 'Data Structure',
              rating: 5,
            },
            {
              title: 'Breadth First Search',
              category: 'Graph',
              rating: 4,
            },
            {
              title: "Floyd's Cycle",
              category: 'Algorithm',
              rating: 4,
            },
            {
              title: 'Travelling Salesman Problem',
              category: 'Algorithm',
              rating: 5,
            },
            {
              title: 'Bellman Ford',
              category: 'Graph',
              rating: 4,
            },
            {
              title: 'KMP Algorithm',
              category: 'String',
              rating: 5,
            },
        ];
    }
}
  
export interface Tutorial {
    title?: string;
    category?: string;
    rating?: number;
}


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import {CarService} from './carservice';
import {ToastModule} from 'primeng/toast';
import {DeferModule} from 'primeng/defer';
import {TableModule} from 'primeng/table';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        DeferModule,
        ToastModule,
        TableModule,
        HttpClientModule
    ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ],
    providers: [CarService]
})
  
export class AppModule { }


Output:

 

Reference: http://primefaces.org/primeng/defer



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads