What is observable, observer and subscribe in angular?

Amit Sharma picture Amit Sharma · Jul 25, 2018 · Viewed 12.7k times · Source

I am learning angular and i got confuse in these observable, observer and subscribe thing. So please explain.

Answer

DeborahK picture DeborahK · Jul 25, 2018

Here is a simple visual to see the difference:

enter image description here

As seen above ... an Observable is a stream of events or data. They are often returned from Angular methods, such as the http.get and the myinputBox.valueChanges.

Subscribing "kicks off" the observable stream. Without a subscribe (or an async pipe) the stream won't start emitting values. It's similar to subscribing to a newspaper or magazine ... you won't start getting them until you subscribe.

The subscribe method takes in an observer. An observer has three methods:

  • The method to process each time an item is emitted from the observable.

  • The method to process any error that occurs.

  • The method to clean up anything when the observer completes. This last one is seldom used when working with Angular's observables.

Hope this helps.

(And I agree ... trying to see the forest through the trees of the docs is quite a challenge. I understand they are working to improve them.)