Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is .subscribe in Angular?

Tags:

angular

I'm going through angular-tour-of-heroes app, and I encountered .subscribe method in routing. Can someone explain what's going on here?

Link for the app: https://embed.plnkr.co/?show=preview

Inside hero-detail.component.ts file,

ngOnInit(): void {   this.route.paramMap     .switchMap((params: ParamMap) => this.heroService.getHero(+params.get('id')))     .subscribe(hero => this.hero = hero); } 
like image 677
Sree11 Avatar asked Jul 05 '17 09:07

Sree11


People also ask

What is subscribe and observable in Angular?

Both techniques define notification handlers, and use them to process multiple values delivered over time. Subscribing to an observable is equivalent to adding an event listener. One significant difference is that you can configure an observable to transform an event before passing the event to the handler.

What is .subscribe in Angular streams data?

In Angular (currently on Angular-6) . subscribe() is a method on the Observable type. The Observable type is a utility that asynchronously or synchronously streams data to a variety of components or services that have subscribed to the observable.

What is subscribe and observable?

Subscribing to an Observable is like calling a function, providing callbacks where the data will be delivered to. This is drastically different to event handler APIs like addEventListener / removeEventListener . With observable. subscribe , the given Observer is not registered as a listener in the Observable.

What is Angular observable pipe?

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.


1 Answers

.subscribe is not an Angular2 thing.

It's a method that comes from rxjs library which Angular is using internally.

If you can imagine yourself subscribing to a newsletter, every time there is a new newsletter, they will send it to your home (the method inside subscribe gets called).

That's what happens when you subscribing to a source of magazines ( which is called an Observable in rxjs library)

All the AJAX calls in Angular are using rxjs internally and in order to use any of them, you've got to use the method name, e.g get, and then call subscribe on it, because get returns and Observable.

Also, when writing this code <button (click)="doSomething()">, Angular is using Observables internally and subscribes you to that source of event, which in this case is a click event.

Back to our analogy of Observables and newsletter stores, after you've subscribed, as soon as and as long as there is a new magazine, they'll send it to you unless you go and unsubscribe from them for which you have to remember the subscription number or id, which in rxjs case it would be like :

 let subscription = magazineStore.getMagazines().subscribe(    (newMagazine)=>{           console.log('newMagazine',newMagazine);      });  

And when you don't want to get the magazines anymore:

   subscription.unsubscribe(); 

Also, the same goes for

 this.route.paramMap 

which is returning an Observable and then you're subscribing to it.

My personal view is rxjs was one of the greatest things that were brought to JavaScript world and it's even better in Angular.

There are 150~ rxjs methods ( very similar to lodash methods) and the one that you're using is called switchMap

like image 157
Milad Avatar answered Oct 13 '22 23:10

Milad