Bird
0
0

Which is the correct way to make an Angular service a singleton using the @Injectable decorator?

easy📝 Syntax Q12 of 15
Angular - Services and Dependency Injection
Which is the correct way to make an Angular service a singleton using the @Injectable decorator?
A@Injectable({ scope: 'singleton' })
B@Injectable({ providedIn: 'root' })
C@Injectable({ singleton: true })
D@Injectable({ providedIn: 'component' })
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular service singleton syntax

    Angular uses providedIn: 'root' to provide a singleton service at the app root level.
  2. Step 2: Check each option's validity

    Only @Injectable({ providedIn: 'root' }) uses the correct syntax. Others are invalid or do not exist in Angular.
  3. Final Answer:

    @Injectable({ providedIn: 'root' }) -> Option B
  4. Quick Check:

    Singleton service = providedIn: 'root' [OK]
Quick Trick: Use providedIn: 'root' for singleton services [OK]
Common Mistakes:
MISTAKES
  • Using non-existent properties like singleton or scope
  • Confusing component-level providers with root-level
  • Omitting the @Injectable decorator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes