Created
February 7, 2018 01:31
-
-
Save wen-dell/aee37777967d7cb3dbaa0809a29b7877 to your computer and use it in GitHub Desktop.
Pluralizing words in Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ | |
name: 'plural' | |
}) | |
export class PluralPipe implements PipeTransform { | |
transform(value: any, args?: any): any { | |
return value > 1 ? `${value} ${args}s` : `${value} ${args}`; | |
} | |
// How to use it in a template: | |
// <p>There are {{ quantity | plural:"student" }} in my school</p> | |
// The word 'student' will be pluralized if the quantity is bigger than 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment