19 April 2020

Service in angular

Service: Service is a class, witch is collection of properties and methods, witch contain re-usable programming logic , witch mainly contains Data source interaction (API Call) and business logic.


  • Services can be accessible in components, directives or pipes.
  • Services can be accessible from other Services.
Steps to create Service:
  • Create Service class
    • Create a class with one or more properties and methods that contains business logic and data access logic.
  • Make ready the service for dependency injection:
    • Add @Injectable() decorator above the service class.
  • Provide the service globally/locally: we have 4 below methods
    • Add the provideIn:"root" option in @Injectable() decorator.
    • Add provides:[service] in AppModule's metadata.
    • Add provides:[service] in any other module's metadata.
    • Add provides:[service] in any other component's metadata.
  • Inject the service into actual component:
    • Add @Inject(service) private referenceVariale : Service in any  component's constructor.

No comments:

Post a Comment