ANGULAR Services

Services are used to implement common functionality across modules.
For eg:-it is most commonly used for database activity through http client package and also you can use for logging errors in the application.
Enter this command in Angular command line interface ,
ng g service mycustomservice
This will create 2 files,

  • mycustomservice.service.ts
  • mycustomservice.service.spec.ts

Now this service needs to be called in app.module.ts
providers: [mycustomservice]
you will have some method in your service and you need to call this method in component.
For eg:- if you want to call from app.component.ts,
then import { mycustomservice } from './mycustomservice.service'
and in constructor you need to inject the service using dependency injection principle.
constructor(private objcustomservice: mycustomservice) {}
In app.component.ts or some other component, you need to call this service methods as mentioned by the following code.
export class app{
constructor(private objcustomservice: mycustomservice) {}