
Lesson 08 · 12 min
HttpClient in a service
The component doesn't know the URL. The service returns an Observable; the template draws it via the async pipe.
this.http.get<User[]>("/api/users") returns an Observable: the request goes out when you subscribe. In the template | async subscribes and unsubscribes by itself. In the class, subscribe without unsubscribing on destroy is a leak. Catch errors with catchError in the service or show a branch in the template.
Sign in to continue
The rest of this lesson opens after sign up
The first lesson of every course is open without an account. To read on, solve the practice and use Ask, create a free account or log in.