DeferDefer directive postpones the loading the content that is initially not in viewport until it becomes visible on scroll. Think of pDefer as a smart ngIf that lazily loads content when it becomes visible after page scroll. Scroll down to load the DataTable which initiates a query that is not executed on initial page load to speed up load performance.

Defer - 图1

Documentation

Import

  1. import {DeferModule} from 'primeng/defer';
  2.  

Getting Started

Defer is applied to a container element with pDefer directive where content needs to be placed inside an ng-template.

  1. <div pDefer>
  2. <ng-template>
  3. deferred content
  4. </ng-template>
  5. </div>
  6.  

Callback

onLoad callback is useful to initialize the content when it becomes visible on scroll such as loading data.

  1. <div pDefer (onLoad)="initData()">
  2. <ng-template>
  3. <p-dataTable [value]="cars">
  4. <p-column field="vin" header="Vin"></p-column>
  5. <p-column field="year" header="Year"></p-column>
  6. <p-column field="brand" header="Brand"></p-column>
  7. <p-column field="color" header="Color"></p-column>
  8. </p-dataTable>
  9. </ng-template>
  10. </div>
  11.  
  1. this.cars = //initialize
  2.  

Properties

Directive has no attributes.

Events

NameParametersDescription
onLoad-Callback to invoke when deferred content is loaded.

Styling

Directive does not apply any styling to host.

Dependencies

None.

Source

View on GitHub

  1. <div pDefer (onLoad)="initData()">
  2. <ng-template>
  3. <p-table [value]="cars">
  4. <ng-template pTemplate="header">
  5. <tr>
  6. <th>Vin</th>
  7. <th>Year</th>
  8. <th>Brand</th>
  9. <th>Color</th>
  10. </tr>
  11. </ng-template>
  12. <ng-template pTemplate="body" let-car>
  13. <tr>
  14. <td>{{car.vin}}</td>
  15. <td>{{car.year}}</td>
  16. <td>{{car.brand}}</td>
  17. <td>{{car.color}}</td>
  18. </tr>
  19. </ng-template>
  20. </p-table>
  21. </ng-template>
  22. </div>
  23.  
  1. export class DeferDemo {
  2. cars: Car[];
  3. constructor(private carService: CarService, private messageService: MessageService) {}
  4. initData() {
  5. this.messageService.add({severity:'success', summary:'Data Initialized', detail:'Render Completed'});
  6. this.carService.getCarsSmall().then(cars => this.cars = cars);
  7. }
  8. }
  9.