HTTP
The categories are currently hardcoded values; however, they should be loaded from the RESTful API and through HTTP calls. Angular provides its own abstraction for HTTP, in the form of a service that you can use when necessary.
Next, let's use Angular's HttpClient
service to load the categories from the backend RESTful API:
- Import
HttpClientModule
intoapp.module.ts
to make theHttpClient
service available to the entire app:
… import { HttpClientModule } from '@angular/common/http'; … @NgModule({ … imports: [ HttpClientModule, … ], … })
- Use the
HttpClient
service inCategoriesService
as follows. If the HTTP address differs in your setup, then make sure to change it accordingly:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { CoreModule } from '../core.module'; import { Category } from '../../../model'; @Injectable({ providedIn: CoreModule, }) export class CategoriesService { constructor(private readonly http: HttpClient...