Search code examples
angulardependency-injectionangular2-services

extending specific services to child class in angular 4


If I want to use services in child class then I have to add those services in parent class also and this results in the availability of those services to all the child classes of that parent class . How o restrict this in Angular 4


Solution

  • Try injecting the service using the providers array inside the declaration itself. For example, you can inject a service called MyService inside AppComponent in the below manner.

    Hope it helps.

    @Component({
      selector: 'my-app',
      providers: [MyService]
    })
    export class AppComponent { }
    

    For a simple example, please refer to https://v4.angular.io/api/core/Directive#providers