Angular & Signals (283 Qs)•Part 9: NgModules, Providers, NgZone & Injector Hierarchies (213 - 255)
How do you configure injectors with providers at different levels?
You can configure injectors with providers at different levels of your application by setting a metadata value. The configuration can happen in one of three places,
- In the
@Injectable()decorator for the service itself - In the
@NgModule()decorator for an NgModule - In the
@Component()decorator for a component
Practical Code Example
typescript
// Provider Configuration at Different Levels:
// 1. Root level: @Injectable({ providedIn: 'root' }) -> Single app-wide instance
// 2. Component level: @Component({ providers: [LocalService] }) -> Isolated instance per component