Bird
0
0

Which of the following is the correct syntax to provide a service only in a specific Angular module?

easy📝 Syntax Q12 of 15
Angular - Services and Dependency Injection
Which of the following is the correct syntax to provide a service only in a specific Angular module?
AAdd the service to the module's <code>providers</code> array
B@Injectable({ providedIn: 'module' })
C@Injectable({ providedIn: 'root' })
DAdd the service to the component's <code>providers</code> array
Step-by-Step Solution
Solution:
  1. Step 1: Recall how to scope service to a module

    Angular does not support providedIn: 'module'. Instead, you add the service to the module's providers array.
  2. Step 2: Check other options

    @Injectable({ providedIn: 'root' }) scopes service app-wide, Add the service to the component's providers array scopes to component, @Injectable({ providedIn: 'module' }) is invalid syntax.
  3. Final Answer:

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

    Module scope = add to module providers [OK]
Quick Trick: Module scope means add service to module providers array [OK]
Common Mistakes:
MISTAKES
  • Using 'providedIn: module' which is invalid
  • Confusing component providers with module providers
  • Assuming 'root' scopes service to module only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes