Three Effective Methods to Limit ngFor Iteration to Specific Number of Items in Angular

Nov 24, 2025 · Programming · 7 views · 7.8

Keywords: Angular | ngFor | SlicePipe | List Rendering | Performance Optimization

Abstract: This article comprehensively explores three practical approaches to limit the number of items displayed by ngFor directive in Angular applications. By analyzing SlicePipe, ng-container with ngIf conditional rendering, and ng-template template syntax, it delves into the implementation principles, performance characteristics, and applicable scenarios of each method. With concrete code examples, the article helps developers understand how to avoid empty list item display issues and provides best practice recommendations.

Introduction

In Angular development, the *ngFor directive is a core tool for iterating over and rendering list data. However, in real-world projects, we often need to limit the number of displayed items, such as in pagination scenarios, dropdown menus, or performance optimization. This article systematically introduces three effective methods to restrict the iteration count of ngFor.

SlicePipe Method

Using Angular's built-in SlicePipe is the most concise solution. This pipe accepts start and end index parameters and returns a subset of the list.

<li *ngFor="let item of list | slice:0:10" class="dropdown-item" (click)="onClick(item)">
    {{item.text}}
</li>

This method filters the data source directly through the pipe, rendering only the first 10 items and completely avoiding the generation of empty elements. SlicePipe internally calls JavaScript's slice() method, offering high performance and clean code.

ng-container with ngIf Conditional Rendering

For scenarios requiring more complex conditional control, ng-container can be used as a logical container:

<ng-container *ngFor="let item of list; let i = index">
    <li class="dropdown-item" (click)="onClick(item)" *ngIf="i < 10">{{item.text}}</li>
</ng-container>

ng-container itself does not generate additional DOM elements and serves only for logical grouping. By controlling the *ngIf condition with the index variable i, it ensures that only the first 10 items are actually rendered.

ng-template Template Syntax

Angular also provides a lower-level ng-template syntax to achieve the same functionality:

<ng-template ngFor let-item [ngForOf]="list" let-i="index">
    <li class="dropdown-item" (click)="onClick(item)" *ngIf="i < 10">{{item.text}}</li>
</ng-template>

This method offers maximum flexibility and is suitable for complex scenarios requiring custom iteration logic. ng-template allows developers to fully control the template instantiation process.

Performance Comparison and Best Practices

Each of the three methods has its advantages: SlicePipe is best for simple slicing needs with the most concise code; ng-container is suitable for complex scenarios requiring conditional rendering; ng-template provides the greatest flexibility but with slightly more verbose code.

From a performance perspective, SlicePipe typically offers the best performance as it filters at the data level. Methods based on conditional rendering, while powerful, create more Angular directive instances.

Component Function Method Supplement

In addition to template-level solutions, filtering functions can be defined in the component:

getLimitedItems(start: number, end: number): any[] {
    return this.list.slice(start, end);
}

Usage in template:

<li *ngFor="let item of getLimitedItems(0, 10)" class="dropdown-item" (click)="onClick(item)">{{item.text}}</li>

This approach fully encapsulates business logic within the component, facilitating testing and maintenance, and is particularly suitable for scenarios requiring dynamic calculation of display ranges.

Conclusion

Limiting the iteration count of ngFor is a common requirement in Angular development. By appropriately choosing among SlicePipe, ng-container, or ng-template solutions, developers can efficiently solve empty list item display issues, enhancing application performance and user experience. It is recommended to select the most suitable implementation based on specific scenarios, balancing code conciseness, performance, and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.