Bird
0
0

You want a service instance to be shared only among components declared in a feature module but not globally. How should you provide the service?

hard🚀 Application Q8 of 15
Angular - Services and Dependency Injection
You want a service instance to be shared only among components declared in a feature module but not globally. How should you provide the service?
AAdd the service to the root module's <code>providers</code> array.
BUse <code>@Injectable({ providedIn: 'root' })</code>.
CAdd the service to the feature module's <code>providers</code> array.
DAdd the service to each component's <code>providers</code> array.
Step-by-Step Solution
Solution:
  1. Step 1: Identify scope requirement

    The service should be shared only within the feature module, not globally.
  2. Step 2: Choose correct provider location

    Providing the service in the feature module's providers array scopes it to that module only.
  3. Final Answer:

    Add the service to the feature module's providers array. -> Option C
  4. Quick Check:

    Module providers scope service to that module only [OK]
Quick Trick: Module providers scope service to that module only [OK]
Common Mistakes:
MISTAKES
  • Using providedIn: 'root' for module-only scope
  • Adding service to root module providers
  • Providing service in each component separately

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes