Challenge - 5 Problems
Angular Injectable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
What does the providedIn property in @Injectable control?
In Angular, the
@Injectable decorator can have a providedIn property. What does this property control?Attempts:
2 left
💡 Hint
Think about how Angular knows where to create and share the service instance.
✗ Incorrect
The
providedIn property tells Angular which injector will provide the service. For example, providedIn: 'root' makes the service a singleton available application-wide.❓ component_behavior
intermediate1:30remaining
What happens if you omit providedIn in @Injectable?
Consider this Angular service without
providedIn:
@Injectable()
export class MyService {}
What is the effect of omitting providedIn?Attempts:
2 left
💡 Hint
Think about how Angular knows where to create the service instance if not told explicitly.
✗ Incorrect
Without
providedIn, Angular does not know where to provide the service. You must add it to a module's providers array manually.📝 Syntax
advanced1:30remaining
Which @Injectable syntax correctly provides a service in the root injector?
Select the correct Angular service declaration that provides the service in the root injector using
@Injectable:Attempts:
2 left
💡 Hint
Check the exact spelling of the property name in the decorator.
✗ Incorrect
The correct property name is
providedIn. Other spellings cause syntax errors or are ignored.❓ state_output
advanced1:30remaining
How many instances of a service with providedIn: 'root' are created?
If a service is declared with
@Injectable({ providedIn: 'root' }) and injected into multiple components, how many instances of the service exist at runtime?Attempts:
2 left
💡 Hint
Think about the meaning of the root injector in Angular.
✗ Incorrect
The root injector creates a singleton instance of the service shared by all components and modules.
🔧 Debug
expert2:00remaining
Why does this service cause a runtime error?
Given this Angular service:
@Injectable({ providedIn: 'module' })
export class LoggerService {
constructor() { console.log('Logger created'); }
}
And this component:
@Component({ selector: 'app-test', template: '' })
export class TestComponent {
constructor(private logger: LoggerService) {}
}
When running the app, a runtime error says the service cannot be found. What is the most likely cause?Attempts:
2 left
💡 Hint
Check if 'module' is a valid value for providedIn and how Angular provides services.
✗ Incorrect
The value 'module' is not a valid option for providedIn. To use a custom injector scope, the service must be added to a providers array. Without that, Angular cannot inject it.