Services in Angular
58
Angular Services
• A service is not an Angular specific concept, rather a pattern of design. • It is a class containing a set of functions that can be used on a
component level.
• We will look at a data service in this section but other services can be created as well
• A data service is the set of function that allow the application to read and manipulate the data
• Usually includes the Create, Read, Update, and Delete operations
59
Adding Service to the Application
• To generate an Angular service from CLI:
ng generate service
• To register it to our application:
providers: [service_name] in the app.module file
OR
Import { Injectable } from @angular/core @Injectable { ProvidedIn: ‘root’ } in the service.ts file
• Both of This will tell angular to instantiate an instance of this service for use by things in this NgModule and any NgModules down the tree
60