Angular Pipe is used to transform a data/value into desired output in the HTML page.
It takes data as input and transforms the data into expected output.
For example, the syntax of the angular pipe is given below,
{{data | pipe}}
{{Hello world | lowercase }}
There are lots of inbuilt pipes and we can create custom pipes as well.
Assume that you have an array of name and want to join it with a comma symbol. Refer below to know how to do that.
First thing is to create join.ts file and copy the below code in it.
join.ts
import {Pipe, PipeTransform} from '@angular/core'
@Pipe ({
name : 'join'
})
export class JoinPipe implements PipeTransform {
public transform(input: string[]): string {
return input ? input.join(",") : ""
}
}
Then add the above pipe in app.module.ts as below.
...
import {Join} from "./pipe/join.pipe"
....
declarations: [Join]
....
export class AppModule {
}
Assume that app.component.ts contains a string array named as “names”. I have not given the app.component.ts file here.
Update the app.componenet.html as below to make use of the join pipe to combine the names.
All names : {{ names | join }