Creating a todos service

Next, we will create a service which will handle the calls and connect our component to the express API. To generate the service with Angular-CLI:

ng generate service todos

The service is created but not registered—to register it in our app, we need to add it to the providers section of the main application module.

Angular-CLI does not automatically register services.

Add the TodosService to the providers array:

import {TodosService} from './todos.service';...providers: [TodosService],...})export class AppModule { }

Now, within our service, we will make HTTP calls to the express server to execute our CRUD operations. First, we will import HTTP, Headers, and rxjs/add/operator/map:

import { Injectable } from '@angular/core'; ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.