Open In App

Angular10 NgPluralCase Directive

In this article, we are going to see what is NgPluralCase in Angular 10 and how to use it.

The NgPluralCase in Angular10 is used to create a view that will be added or removed from the parent NgPlural when the given expression matches the plural expression. We can use the values to make the output plural or singular based on the conditions.



Syntax:

<li *NgPluralCase='condition'></li>

NgModule: Module used by NgPluralCase is:



 

Selectors:

Approach: 

Example 1:




import { Component } from '@angular/core';
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    val = 1;
}




<some-element [ngPlural]="val">
    <ng-template ngPluralCase="=1">Geek</ng-template>
    <ng-template ngPluralCase="=2">Geeks</ng-template>
</some-element>

Output:

Geek

Example 2:




import { Component } from '@angular/core';
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    valcar = 5;
}




<some-element [ngPlural]="valcar">
    <ng-template ngPluralCase="=1">{{valcar}} Car</ng-template>
    <ng-template ngPluralCase="=5">{{valcar}} Cars</ng-template>
</some-element>

Output:

5 Cars

Reference: https://angular.io/api/common/NgPluralCase


Article Tags :