tinycodinglm / lang /angular-di.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
1.29 kB
opencode trace
System You are an expert software engineer.

You are an expert software engineer.

User

How does Angular's dependency injection work?

Assistant

Angular DI uses a hierarchical injector system:

NullInjector (ultimate parent)
-> PlatformRef Injector (platform services)
-> EnvironmentInjector (app/module scope, R3Injector)
-> NodeInjector (element-level, LView/TView-based)

inject(token) searches: NodeInjector -> EnvironmentInjector -> PlatformInjector -> NullInjector.

Provider types:

  • Class: { provide: Token, useClass: Class }
  • Value: { provide: Token, useValue: val }
  • Factory: { provide: Token, useFactory: fn }
  • Existing: { provide: Token, useExisting: Other }

Resolution modifiers:

  • Self: only current injector
  • SkipSelf: start at parent
  • Optional: return null instead of throwing
  • Host: stop at host component boundary

NodeInjector uses LView/TView for element-level DI, avoiding full injector tree traversal.
ChainedInjector combines EnvironmentInjector and NodeInjector for performance.