← All Angular Web Framework Flashcard Decks

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
  1. What is an InjectionToken used for in Angular?

    Answer: To create a type-safe token for non-class dependencies like strings or config objects

    InjectionToken provides a unique, type-safe token for values that cannot be represented by a class, such as configuration objects, strings, or interfaces.

  2. Which `providers` syntax allows you to supply a pre-constructed object or primitive value directly?

    Answer: { provide: TOKEN, useValue: myObject }

    useValue registers a static, pre-built value with the injector so no factory or class instantiation occurs.

  3. How do you declare factory dependencies when using `useFactory`?

    Answer: Provide them via the `deps` array in the provider definition

    The deps array in the provider object lists the tokens Angular resolves and passes as arguments to the factory function in order.

  4. What does the `@Optional()` decorator do when applied to a constructor parameter?

    Answer: Allows the injection to resolve to null if the provider is not registered

    @Optional() prevents an error when a dependency has no registered provider; Angular injects null instead of throwing.

  5. What is the role of the `@Self()` decorator in Angular DI?

    Answer: It restricts resolution to the injector of the current element, never walking up to parent injectors

    @Self() tells Angular to look for the dependency only in the host element's injector and throw if it's not found there.

  6. Which class-level `providers` option in @Component creates a new service instance for every component instance?

    Answer: providers: [MyService] inside @Component metadata

    Adding a service to a component's providers array causes Angular to create a fresh instance scoped to that component and its subtree.

  7. What is the difference between `providers` and `viewProviders` in an Angular component?

    Answer: viewProviders are visible only to the component's own view, not to projected content (ng-content)

    Services in viewProviders are not accessible from content projected via ng-content, whereas providers are visible to both the component's view and projected content.