How do you call an API in Angular?
Model answer
Provide the HTTP client with provideHttpClient() in the app configuration, inject HttpClient into a service, and call methods such as http.get<User[]>('/api/users'). It returns an Observable, so nothing is sent until someone subscribes (or the async pipe or toSignal does).
Put API calls in services, not in components, so they are reusable and testable. Handle loading and error states in the UI.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is an Observable, and how is it different from a Promise?
- What is the difference between subscribe and the async pipe?
- What are common RxJS operators you use, and what do they do?
- Explain the difference between switchMap, mergeMap, concatMap and exhaustMap.
- What are HTTP interceptors, and what are they used for?
- How do you handle errors from HTTP calls?