This project is a hands-on Angular 19 playground that demonstrates core and advanced Dependency Injection (DI) patterns side-by-side.
- Angular 19 (standalone components)
- TypeScript 5
- RxJS 7
- Karma + Jasmine for unit tests
- Install dependencies:
npm install- Start the app:
npm start- Open:
http://localhost:4200
npm start- Run dev server (ng serve)npm run build- Production buildnpm run watch- Development build in watch modenpm test- Run unit tests
- Root route (
/) renders the DI demo dashboard with all cards. - Named outlet route (
/(dynamic:lazy)) renders the lazy DI demo. - Header buttons in
AppComponentswitch between Home and Lazy views.
BasicInjectionComponentinjectsLoggerServiceand shows instance ID.
InjectFunctionComponentusesinject(LoggerService)and logs a message.
SingletonCptAComponentandSingletonCptBComponentboth consume the sameLoggerService(providedIn: 'root').
ComponentProviderComponentdeclaresproviders: [LoggerService], creating a local instance per component.
TokenInjectionComponentprovidesAPI_URLtoken and injects it.
FactoryProviderComponentprovidesLoggerServicevialoggerFactory, using an injected'API_URL'dependency.
ParentComponentprovidesLoggerService;ChildComponentresolves the same parent-scoped instance.
OptionalDiComponentusesinject(OptionalService, { optional: true }).
EnvironmentInjectorComponentresolvesLoggerServicedynamically withEnvironmentInjector#get.
MultiProviderComponentregistersMULTI_LOGGERwith two implementations (ConsoleLoggerService,FileLoggerService) usingmulti: true.
InterceptorDiComponentmakesHttpClientcalls.main.tswiresprovideHttpClient(withInterceptors([...]))with:authInterceptorloggingInterceptorerrorInterceptor
SingletonVsComponentProviderComponentrenders both patterns in one view to compare instance sharing vs per-component instances.
LazyModuleDiComponentis loaded through a named router outlet and resolvesLoggerServicein the lazy route context.
src/main.ts- App bootstrap + HTTP interceptor providerssrc/app/app.routes.ts- Home + lazy routessrc/app/services/logger.service.ts- Core logger service used across demossrc/app/tokens/api.token.ts- Typed injection token examplesrc/app/tokens/multi-logger.token.ts- Multi-provider token examplesrc/app/components/home/home.component.html- Dashboard composition of DI demo cards
- Most demos display a logger instance ID so you can visually confirm injector scope.
- The app is built for learning and experimentation rather than production architecture.
