What is pipe() function in Angular
Solution 1:
Don't get confused with the concepts of Angular and RxJS
We have pipes concept in Angular and pipe()
function in RxJS.
1) Pipes in Angular: A pipe takes in data as input and transforms it to the desired output
https://angular.io/guide/pipes
2) pipe()
function in RxJS: You can use pipes to link operators together. Pipes let you combine multiple functions into a single function.
The pipe()
function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.
https://angular.io/guide/rx-library (search for pipes in this URL, you can find the same)
So according to your question, you are referring pipe()
function in RxJS
Solution 2:
The Pipes you are talking about in the starting description are different from the pipe you showed in the example.
In Angular(2|4|5) Pipes are used to format view as you said. I think you have a basic understanding of pipes in Angular, you can learn more about that from this link - Angular Pipe Doc
The pipe()
you have shown in the example is the pipe()
method of RxJS 5.5 (RxJS is the default for all Angular apps). In Angular5 all the RxJS operators can be imported using single import and they are now combined using the pipe method.
tap()
- RxJS tap operator will look at the Observable value and do something with that value. In other words, after a successful API request, the tap()
operator will do any function you want it to perform with the response. In the example, it will just log that string.
catchError()
- catchError does exactly the same thing but with error response. If you want to throw an error or want to call some function if you get an error, you can do it here. In the example, it will call handleError()
and inside that, it will just log that string.
Solution 3:
RxJS Operators are functions that build on the observables foundation to enable sophisticated manipulation of collections.
For example, RxJS defines operators such as map()
, filter()
, concat()
, and flatMap()
.
You can use pipes to link operators together. Pipes let you combine multiple functions into a single function.
The pipe()
function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.
Solution 4:
Two very different types of Pipes Angular - Pipes and RxJS - Pipes
Angular-Pipe
A pipe takes in data as input and transforms it to a desired output. In this page, you'll use pipes to transform a component's birthday property into a human-friendly date.
import { Component } from '@angular/core';
@Component({
selector: 'app-hero-birthday',
template: `<p>The hero's birthday is {{ birthday | date }}</p>`
})
export class HeroBirthdayComponent {
birthday = new Date(1988, 3, 15); // April 15, 1988
}
RxJS - Pipe
Observable operators are composed using a pipe method known as Pipeable Operators. Here is an example.
import {Observable, range} from 'rxjs';
import {map, filter} from 'rxjs/operators';
const source$: Observable<number> = range(0, 10);
source$.pipe(
map(x => x * 2),
filter(x => x % 3 === 0)
).subscribe(x => console.log(x));
The output for this in the console would be the following:
0
6
12
18
For any variable holding an observable, we can use the .pipe() method to pass in one or multiple operator functions that can work on and transform each item in the observable collection.
So this example takes each number in the range of 0 to 10, and multiplies it by 2. Then, the filter function to filter the result down to only the odd numbers.
Solution 5:
You have to look to official ReactiveX documentation: https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md.
This is a good article about piping in RxJS: https://blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44.
In short .pipe() allows chaining multiple pipeable operators.
Starting in version 5.5 RxJS has shipped "pipeable operators" and renamed some operators:
do -> tap
catch -> catchError
switch -> switchAll
finally -> finalize