InterviewHubPRO
Section Overview
Q234

Question 234 of 283

Part 9: NgModules, Providers, NgZone & Injector Hierarchies (213 - 255)

83%
Angular & Signals (283 Qs)•Part 9: NgModules, Providers, NgZone & Injector Hierarchies (213 - 255)

How do you restrict provider scope to a module?

It is possible to restrict service provider scope to a specific module instead making available to entire application. There are two possible ways to do it.

  1. Using providedIn in service:
js
  import { Injectable } from '@angular/core';
  import { SomeModule } from './some.module';

  @Injectable({
    providedIn: SomeModule,
  })
  export class SomeService {
  }
  1. Declare provider for the service in module:
js
  import { NgModule } from '@angular/core';

  import { SomeService } from './some.service';

  @NgModule({
    providers: [SomeService],
  })
  export class SomeModule {
  }