From 96a424820278b2a7532f1a2272b1ff15826ba2a8 Mon Sep 17 00:00:00 2001 From: Wei Hung Chin Date: Fri, 28 Jun 2019 10:34:24 +0800 Subject: [PATCH] Add new sub-section about using pipes in component (#30152) Add example of using custom pipe programmatically in a component method --- guide/english/angular/pipes/index.md | 41 +++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/guide/english/angular/pipes/index.md b/guide/english/angular/pipes/index.md index f37abfbc87..74d7e72d83 100644 --- a/guide/english/angular/pipes/index.md +++ b/guide/english/angular/pipes/index.md @@ -136,7 +136,46 @@ export class AppComponent {
{{ someValue | example:‘lowercase’ }}
``` -Understanding the above example means you understand Angular pipes. There is only one more topic left to discuss. +### Using Pipes in Components or Services +Other than using pipes in the html template as detailed above, we also can call the pipe programmatically in our components or services. + +Consider the `ExamplePipe` created above. To use it in a component, we need to provide it to the `@NgModule` where our component is declared. + +```typescript +// app.module.ts +import { ExamplePipe } from 'example.pipe'; + +@NgModule({ + ... + providers: [ExamplePipe], + ... +}) +export class AppModule { } +``` + +In our component, we need to inject it into the constructor. Then we simply call the transform method on the pipe and passing in the arguments like so: + +```typescript +// app.component.ts +import { ExamplePipe } from 'example.pipe'; + +@Component({ + templateUrl: 'app.component.html' +}) +export class AppComponent { + + constructor(private examplePipe:ExamplePipe) + someValue:string = "HeLlO WoRlD!"; + + // we can call toUpperCase programmatically to convert the string to uppercase + toUpperCase(){ + this.someValue = this.examplePipe.transform(this.someValue, 'uppercase'); + } + +} +``` + +Understanding the above examples means you understand Angular pipes. There is only one more topic left to discuss. #### Pure and Impure Pipes