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
✗ Incorrect
A service provided in root is a singleton shared across the entire app.
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
✗ Incorrect
Providing a service in a module's providers array limits its scope to that module.
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
✗ Incorrect
Providing a service in a component creates a new instance for that component and its children.
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
✗ Incorrect
Angular uses hierarchical injection and picks the closest provider in the component tree.
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
✗ Incorrect
Root services are singletons and share state across the app, so they don't isolate state per component.
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.
Practice
(1/5)
1. What does setting a service's providedIn property to 'root' mean in Angular?
easy
A. The service is a singleton shared across the entire application.
B. The service is created separately for each module.
C. The service is only available inside a specific component.
D. The service is not injectable anywhere.
Solution
Step 1: Understand providedIn: 'root' meaning
This setting makes Angular create one instance of the service for the whole app.
Step 2: Compare with other scopes
Unlike module or component scopes, 'root' shares the same instance everywhere.
Final Answer:
The service is a singleton shared across the entire application. -> Option A
Quick Check:
Service scope 'root' = singleton app-wide [OK]
Hint: Root scope means one service instance for whole app [OK]
Common Mistakes:
Thinking 'root' creates multiple instances
Confusing 'root' with component scope
Assuming 'root' limits service to one module
2. Which of the following is the correct syntax to provide a service only in a specific Angular module?
easy
A. Add the service to the module's providers array
B. @Injectable({ providedIn: 'module' })
C. @Injectable({ providedIn: 'root' })
D. Add the service to the component's providers array
Solution
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.
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.
Final Answer:
Add the service to the module's providers array -> Option A
Quick Check:
Module scope = add to module providers [OK]
Hint: Module scope means add service to module providers array [OK]
Common Mistakes:
Using 'providedIn: module' which is invalid
Confusing component providers with module providers
If two instances of app-example are rendered, what will be the value of counter in each?
medium
A. Both will show 0 because service is not provided.
B. Both will show the same incremented value shared globally.
C. Both will show 1 because each has its own service instance.
D. Both will show an error because service is missing.
Solution
Step 1: Understand component-level service scope
Providing CounterService in providers inside the component creates a new instance per component.
Step 2: Analyze two component instances
Each component instance gets its own service instance, so each counter starts fresh and increments once to 1.
Final Answer:
Both will show 1 because each has its own service instance. -> Option C
Quick Check:
Component scope = separate service instances per component [OK]
Hint: Component providers create separate service instances per component [OK]
Common Mistakes:
Assuming service is shared globally despite component providers
Thinking service is not provided at all
Confusing component scope with root scope
4. You have a service provided in a feature module's providers array. However, when injecting it in a lazy-loaded module, Angular throws an error that the service is not found. What is the likely cause?
medium
A. The service is provided in the root injector, causing conflicts.
B. The lazy-loaded module has its own injector and does not see the feature module's providers.
C. The service is declared but not exported from the module.
D. The service class is missing the @Injectable decorator.
Solution
Step 1: Understand lazy-loaded module injectors
Lazy-loaded modules have their own injectors and do not inherit providers from eagerly loaded modules.
Step 2: Identify provider visibility issue
Since the service is provided in a feature module not loaded by the lazy module, the lazy module injector can't find it.
Final Answer:
The lazy-loaded module has its own injector and does not see the feature module's providers. -> Option B
Quick Check:
Lazy modules have separate injectors = missing providers [OK]
Hint: Lazy modules have isolated injectors; providers must be re-declared [OK]
Common Mistakes:
Assuming root injector shares providers with lazy modules
Forgetting to add @Injectable decorator
Confusing export with provider visibility
5. You want to create a logging service that keeps a separate log for each component instance but also shares some global settings. Which service scope setup is best?
hard
A. Provide the service in root and use a static variable for logs.
B. Provide the service in root and use internal state to separate logs per component.
C. Provide the service only in a feature module's providers array.
D. Provide the service in each component's providers array and inject a global settings service from root.
Solution
Step 1: Separate logs per component instance
Providing the logging service in each component's providers creates a new instance per component, isolating logs.
Step 2: Share global settings
Injecting a global settings service provided in root allows sharing common configuration across all logging instances.
Final Answer:
Provide the service in each component's providers array and inject a global settings service from root. -> Option D
Quick Check:
Component scope for logs + root scope for settings = best mix [OK]
Hint: Use component scope for instance data, root scope for shared config [OK]
Common Mistakes:
Using root scope for logs causes shared state issues
Providing only in module loses per-component isolation
Using static variables breaks instance separation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.