Using Angular observable pipe with example

The pipe method of the Angular Observable is used to chain multiple operators together. We can use the pipe as a standalone method, which helps us to reuse it at multiple places or as an instance method. In this tutorial, we will take a look at the pipe and learn how to use it in an Angular Application. We will show you examples of pipe using map, filter & tap operators.

RxJs Operators

The operators are very important components of the Rxjs library. They are functions that take an observable as input and transform it into a new observable and return it. We use them to manipulate the observable data stream.

For Example.

Map operator applies a given project function to each value emitted by the source Observable and emits the resulting values as an Observable.

Filter operator filter items from the source observable based on some condition and returns the filtered value as a new observable

The RxJS Operator transforms the source into a new observable. Use pipe to invoke operators

The following table lists some of the commonly used operators

AREAOPERATORS
CombinationcombineLatest, concat, merge, startWith , withLatestFrom, zip
FilteringdebounceTime,
distinctUntilChanged, filter,
take, takeUntil, takeWhile, takeLast, first, last, single, skip, skipUntil, skipWhile, skipLast,
TransformationbufferTime, concatMap, map, mergeMap, scan, switchMap, ExhaustMap, reduce
Utilitytap, delay, delaywhen
Error Handlingthrowerror, catcherror, retry, retrywhen
Multicastingshare

Using pipe to combine operators

The pipe method accepts operators such as filter, map, as arguments. Each argument must be separated by a comma. The order of the operators is important because when a user subscribes to an observable, the pipe executes the operators in a sequence in which they are added.

There are two ways we can use the pipe. One as an instance of observable and the other way is to use if as standalone method

To use observable we need it to import from the rxjs library. If you are intend to use the pipe standalone function, then you also need to import it as well. All the operators are available in the library rxjs/operators.

Pipe as an instance method

The pipe as an instance method is used as below. We the operators op1, op2 etc are passed as the argument to pipe method. The output of op1 method becomes input of the op2 operator and so forth.

Example : Pipe with Map, Filter & Tap

Here is the example of using pipe with map & filter operator.

The following example makes use of pipe with map, filter & tap operator. The tap operator returns a new observable which is a mirror copy of the source observable. We use it mostly for debugging purposes ( for example for logging the values of observable as shown below).

Pipe as stand alone method

We can also use the pipe as a standalone function to compose operators and re use the pipe at other places.

Example

You can also use the stand alone pipe as shown below.

References

  1. RxJs Library
  2. Operators
  3. Pipe API
  4. Map API
  5. Tap API
  6. Filter API

8 thoughts on “Using Angular observable pipe with example”

  1. This website is awesome. I have been searching for this information for long. All the answers that I got are confusing and pure theoretical which is very hard to understand. This website is giving easy answers for the complicated angular questions. Much appreciated.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top