0
0
Angularframework~5 mins

Service scope (root, module, component) in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean when a service is provided in the root scope in Angular?
A service provided in the root scope is a singleton shared across the entire application. It is created once and available everywhere.
Click to reveal answer
intermediate
How does providing a service in a module affect its scope?
When a service is provided in a module, it is shared only among components and other services within that module. It is not global to the whole app.
Click to reveal answer
intermediate
What happens if a service is provided in a component's providers array?
Providing a service in a component creates a new instance of that service for that component and its children. This means each component instance gets its own service instance.
Click to reveal answer
intermediate
Why might you want to provide a service at the component level instead of root?
Providing a service at the component level allows each component to have its own independent service instance. This is useful for isolating state or behavior per component.
Click to reveal answer
advanced
How does Angular decide which service instance to inject when multiple scopes provide the same service?
Angular uses hierarchical injection. It starts looking from the component's injector up to the root. The closest provider in the hierarchy is used.
Click to reveal answer
If a service is provided in root, how many instances exist in the app?
AOne shared instance
BOne instance per module
COne instance per component
DA new instance every time it is injected
Where do you provide a service to limit its scope only to a specific module?
AIn the component's providers array
BIn the root injector
CIn the module's providers array
DIn the main.ts file
What is the effect of providing a service in a component's providers array?
ACreates a singleton service for the whole app
BCreates a new service instance for that component and its children
CMakes the service unavailable to that component
DOverrides the service globally
How does Angular resolve which service instance to inject when multiple providers exist?
AIt uses the closest provider in the component hierarchy
BIt picks a random instance
CIt always uses the root instance
DIt throws an error
Why might you avoid providing a service in root if you want isolated state?
ABecause root services cannot be lazy loaded
BBecause root services are not injectable
CBecause root services are slower
DBecause root services share state across the app
Explain the differences between providing a service in root, module, and component scopes in Angular.
Think about how many instances of the service exist and where they are shared.
You got /4 concepts.
    Describe a scenario where providing a service at the component level is better than providing it in root.
    Consider when you want separate behavior or data for each component.
    You got /4 concepts.