0
0
Angularframework~5 mins

@Injectable decorator and providedIn in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Injectable decorator in Angular?
The @Injectable decorator marks a class as available to be provided and injected as a dependency in Angular's dependency injection system.
Click to reveal answer
beginner
What does the providedIn property inside @Injectable do?
The providedIn property tells Angular where to provide the service, such as 'root' for application-wide singleton or a specific module, controlling the service's scope and lifetime.
Click to reveal answer
beginner
What happens if you set providedIn: 'root' in @Injectable?
The service is registered in the root injector, making it a singleton available throughout the whole app without needing to add it to any module's providers array.
Click to reveal answer
intermediate
Can you provide a service only in a specific Angular module using providedIn?
Yes, you can set providedIn to a specific module class to limit the service's availability only to that module and its children.
Click to reveal answer
intermediate
Why is using providedIn preferred over adding services to the providers array in Angular modules?
Using providedIn enables tree-shaking, so unused services are removed from the final build, and it simplifies service registration by avoiding manual providers array management.
Click to reveal answer
What does the @Injectable decorator do in Angular?
ARegisters a route in the router
BDefines a component's template
CMarks a class as injectable for dependency injection
DCreates a new Angular module
What is the effect of setting providedIn: 'root' in @Injectable?
AService is available app-wide as a singleton
BService is only available in the current component
CService is not injectable
DService is provided only in lazy-loaded modules
How does providedIn help with tree-shaking?
AIt increases the bundle size
BIt forces all services to be included
CIt disables dependency injection
DIt allows unused services to be removed from the final build
If you want a service only in a specific module, what should you do?
ASet providedIn to 'root'
BSet providedIn to that module's class
CAdd the service to the component's providers array
DUse @Component decorator instead
What happens if you omit providedIn in @Injectable?
AYou must add the service manually to a providers array
BThe service is automatically provided in root
CThe service cannot be injected
DAngular throws a compile error
Explain how the @Injectable decorator and providedIn property work together in Angular services.
Think about how Angular knows where and how to provide a service.
You got /5 concepts.
    Describe the advantages of using providedIn over adding services to module providers arrays.
    Consider build size and ease of use.
    You got /4 concepts.