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.

Get hands-on with 1200+ tech skills courses.