-
-
Save yogain123/b19cee2a4a95176a5f05774811a13c3a to your computer and use it in GitHub Desktop.
hola |
BehaviorSubject
The BehaviorSubject holds the value that needs to be shared with other components. These components subscribe to data which is simple returning the BehaviorSubject value without the functionality to change the value. Here is a more detailed breakdown of asObservable. In updateDataSelection we call next and pass in a new value to the BehaviorSubject.
All subscribers to a subject share the same execution of the subject. i.e. when a subject produces data, all of its subscribers will receive the same data. This behavior is different from observables, where each subscription causes an independent execution of the observable.
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Injectable } from '@angular/core';
@Injectable()
export class ShoppingkartService {
private totalItems = new BehaviorSubject(null);
hola : number = 0;
constructor() { }
addToShoppingKart(){
this.hola++;
this.totalItems.next(this.hola);
}
getTotalItems(){
return this.totalItems;
}
}
======
In some component
ngOnInit() {
this.shoppingkartService.getTotalItems().subscribe(item=>{
this.totalItems=item;
})
}
WE can Complete Observation using complete
completeMe(){
this. totalItems.complete();
}
Create Your Own Observable
import { Observable } from "rxjs/Observable"
// create observable
const simpleObservable = new Observable((observer) => {
// observable execution
observer.next("bla bla bla")
observer.complete()
})
// subscribe to the observable
simpleObservable.subscribe()
// dispose the observable
simpleObservable.unsubscribe()
Main Difference between Observables and Promise
Observables can have multiple values over time
Now if you keep that subscription to the newsletter open, you will get a new one every once and a while. The sender decides when you get it but all you have to do is just wait until it comes straight into your inbox.
If you come from the world of promises this is a key difference as promises always return only one value. Another thing is that observables are cancelable. If you don’t want your newsletter anymore, you unsubscribe. With promises this is different, you can’t cancel a promise
Subscribe to routeQueryParams
https://www.udemy.com/the-complete-angular-master-class/learn/v4/t/lecture/7824574?start=0
making header and sending request
===================
The Very Same Thing with almost no code can be done using angular AuthHttp