Services & Dependency Injection Flashcards
7 cards from real Angular Web Framework practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Services & Dependency Injection flashcards as text
What does `providedIn: 'any'` do in Angular's @Injectable decorator?
Answer: Creates a separate singleton instance for each lazy-loaded module that injects it
providedIn: 'any' gives each lazy-loaded module its own singleton while all eagerly loaded code shares one instance.
In Angular's hierarchical injection system, what happens when an injector cannot find a requested token?
Answer: It throws a NullInjectorError
When no injector in the hierarchy can satisfy a token and no @Optional() is present, Angular throws a NullInjectorError.
Which Angular injector level sits above the root (AppModule) injector?
Answer: Platform injector
The platform injector is created by platformBrowserDynamic() and sits above the root injector; it holds platform-wide services shared across multiple Angular apps on the same page.
What is the purpose of `@SkipSelf()` in Angular dependency injection?
Answer: Tells Angular to start the injector lookup from the parent injector, bypassing the current one
@SkipSelf() causes Angular to skip the current element's injector and search up the tree starting from the parent, preventing circular use of the current injector.
How does Angular's `forRoot()` pattern prevent duplicate service instances in feature modules?
Answer: It provides services in the module's forRoot() call so only the root import registers them
The forRoot() convention puts service providers in the return value of the static forRoot() method, which is called only once in AppModule, ensuring a single instance.
Which Angular testing utility configures providers for dependency injection in unit tests?
Answer: TestBed.configureTestingModule()
TestBed.configureTestingModule() sets up the testing module, including the providers array where you register real or mock services.
What Angular API retrieves a service instance from the current injector inside a component class?
Answer: inject(ServiceClass) called in an injection context
The inject() function, when called inside a constructor or field initializer (an injection context), retrieves the service from the active injector.