Adding Angular Component and Service for the News API
According to the Angular documentation, a service is a
broad category encompassing any value, function, or feature that an app needs. A service is typically a class with a narrow, well-defined purpose. It should do something specific and do it well.
In our case, we want to connect to our express API developed in the previous sessions and we set up this connection in our service and then inject it into our application whenever we need it.
ng g service services/news
This will generate these two files under our services directory.
In our news.service.ts file, we set up our connection to get the news items from our API in the getNewsByCategory function.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
export const NEWS_API_URL_ENDPOINT = 'http://localhost:3000/api/news/';
@Injectable({
providedIn: 'root',
})
export class NewsService {
constructor(private http: HttpClient) {}
public getNewsByCategory(payload: string) {
return this.http.get(NEWS_API_URL_ENDPOINT + payload);
}
}
Since we want to make our angular application aware of the services we have to import the HttpClientModule into the main app.module.ts as well and make it part of our imports.
Add HttpClientModule to imports in the main app module.
Next, we will generate a new component to load data from our news API
ng g component components/news-items
In this component, we will inject the news service and call the service when the angular component is initialized in the angular cycle ngOnInit. In ngOnInit, we subscribe to our service which returns our news articles data. The component has an input called category which determines what category of news items to load. It also has the property articles where we will store the articles retrieved for the component.