0
0
Angularframework~20 mins

@Injectable decorator and providedIn in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Injectable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What does the providedIn property in @Injectable control?
In Angular, the @Injectable decorator can have a providedIn property. What does this property control?
AIt defines the CSS styles applied to the service's components.
BIt specifies the HTML element where the service will be rendered.
CIt controls where the service is registered in the dependency injection system, determining its scope and lifetime.
DIt sets the HTTP endpoint URL for the service's API calls.
Attempts:
2 left
💡 Hint
Think about how Angular knows where to create and share the service instance.
component_behavior
intermediate
1: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?
AThe service is not automatically provided anywhere and must be added to a module's providers array to be injectable.
BThe service is provided only in the component where it is injected.
CThe service will cause a compile-time error due to missing providedIn.
DThe service is automatically provided in the root injector by default.
Attempts:
2 left
💡 Hint
Think about how Angular knows where to create the service instance if not told explicitly.
📝 Syntax
advanced
1: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:
A
@Injectable({ providedIn: 'root' })
export class DataService {}
B
@Injectable({ provideIn: 'root' })
export class DataService {}
C
@Injectable({ providerIn: 'root' })
export class DataService {}
D
@Injectable({ provided: 'root' })
export class DataService {}
Attempts:
2 left
💡 Hint
Check the exact spelling of the property name in the decorator.
state_output
advanced
1: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?
AOne instance per module that imports the service.
BOne instance per component that injects the service.
CA new instance every time the service is injected.
DOne single instance shared across the entire application.
Attempts:
2 left
💡 Hint
Think about the meaning of the root injector in Angular.
🔧 Debug
expert
2: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?
AThe service is missing from the module's providers array, so it is not injectable.
BThe 'module' value for providedIn is invalid and causes Angular not to provide the service.
CThe service constructor is missing the @Inject decorator.
DThe component selector is invalid, preventing injection.
Attempts:
2 left
💡 Hint
Check if 'module' is a valid value for providedIn and how Angular provides services.