...
/BehaviourSubject and Async Pipe in Angular
BehaviourSubject and Async Pipe in Angular
Now we will learn how to use BehaviourSubject to get the latest values of the search bar and display the photos on the screen using an "async" pipe.
results-list.component.ts
The puzzle’s final piece is results-list.component.ts
. We will import the photo search service in that file and add it to the constructor.
The component will have access to the photo search service, but all the photo search service can do is search. The subscription lies in the header component there’s no way to access it in the results list.
Instead, the photo search service needs to be upgraded using a subject to allow it to share new results with any component that wants to subscribe. In this case, the photo search service uses a BehaviorSubject
.
BehaviourSubject
A BehaviorSubject
is a simplified version of the ReplaySubject
you used back in
Multiplexing Observables.
Whereas the ReplaySubject
stored an arbitrary number of events, the BehaviorSubject
only records the value of the latest event. Whenever a BehaviorSubject
records a new subscription, it emits the latest value to the subscriber as well as any new values that are passed in.
The BehaviorSubject
...