0
0
Angularframework~20 mins

Service scope (root, module, component) in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
Service instance count with root scope

Consider an Angular service provided in root scope. If you inject this service into two different components, how many instances of the service will be created?

AOne instance per module containing the components
BTwo separate instances, one per component
COne single instance shared by both components
DA new instance every time the service is injected
Attempts:
2 left
💡 Hint

Think about the providedIn: 'root' meaning in Angular services.

component_behavior
intermediate
1:30remaining
Service instance behavior with module scope

If a service is provided in a feature module (not root), and two components from that module inject it, how many instances of the service exist?

AOne instance shared by all components in the module
BOne instance per component
COne instance per lazy loaded module only
DOne instance per app regardless of module
Attempts:
2 left
💡 Hint

Consider how Angular creates service instances for modules.

state_output
advanced
2:00remaining
Service instance count with component scope

Given a service provided in a component's providers array, what happens when two instances of that component are created?

AAngular throws an error due to multiple instances
BBoth components share the same service instance
CService instance is shared across all components in the module
DEach component instance gets its own separate service instance
Attempts:
2 left
💡 Hint

Think about how component-level providers work in Angular.

📝 Syntax
advanced
1:30remaining
Correct syntax for providing a service in a module

Which of the following is the correct way to provide a service in an Angular module?

AAdd the service class to the <code>providers</code> array in the module decorator
BAdd the service class to the <code>declarations</code> array in the module decorator
CAdd the service class to the <code>imports</code> array in the module decorator
DAdd the service class to the <code>bootstrap</code> array in the module decorator
Attempts:
2 left
💡 Hint

Remember where Angular expects services to be listed in a module.

🔧 Debug
expert
2:30remaining
Unexpected multiple service instances in lazy loaded modules

You have a service provided in root scope. However, when you lazy load two different modules that inject this service, you notice two separate instances are created. What is the most likely cause?

AThe service is not decorated with <code>@Injectable</code>
BThe service is also listed in the <code>providers</code> array of each lazy loaded module
CThe service is provided only in <code>root</code> and should not create multiple instances
DAngular does not support singleton services with lazy loaded modules
Attempts:
2 left
💡 Hint

Check if the service is accidentally provided in multiple places.