0
0
Angularframework~20 mins

How dependency injection works in Angular - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the role of the injector in Angular's dependency injection?

In Angular, the injector is a core part of dependency injection. What does it do?

AIt creates and provides instances of services when requested by components or other services.
BIt compiles Angular templates into JavaScript code.
CIt manages routing between different pages in the application.
DIt handles user input events like clicks and key presses.
Attempts:
2 left
💡 Hint

Think about who is responsible for giving components the services they need.

component_behavior
intermediate
2:00remaining
What happens if a service is provided in a component's providers array?

Consider a service provided in the providers array of a component. How does Angular handle this service instance?

AAngular uses the same global instance of the service for the entire app.
BAngular creates a new instance of the service only for that component and its children.
CAngular throws an error because services cannot be provided in components.
DAngular creates a new instance of the service for every component in the app.
Attempts:
2 left
💡 Hint

Think about service scope and how providing in a component affects instance sharing.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly injects a service using Angular's constructor injection?

Choose the correct way to inject a service named DataService into a component using Angular's constructor injection.

Angular
import { Component } from '@angular/core';
import { DataService } from './data.service';

@Component({
  selector: 'app-example',
  template: '<p>Example works!</p>'
})
export class ExampleComponent {
  constructor( /* injection here */ ) {}
}
Aconstructor(@Inject(DataService) private dataService: DataService) {}
Bconstructor(dataService: DataService) { this.dataService = dataService; }
Cconstructor(private dataService) {}
Dconstructor(private dataService: DataService) {}
Attempts:
2 left
💡 Hint

Remember how TypeScript and Angular use constructor parameters with access modifiers for injection.

lifecycle
advanced
2:00remaining
When is a service instance created by Angular's injector?

At what point does Angular create an instance of a service provided in the root injector?

AWhen the first component or service requests it (lazy creation).
BImmediately when the app starts, before any component loads.
COnly when the service is manually instantiated in code.
DWhen the Angular compiler processes the service file.
Attempts:
2 left
💡 Hint

Think about when Angular actually needs the service instance.

🔧 Debug
expert
3:00remaining
Why does this Angular component fail to inject the service?

Given this component code, why does Angular throw an error about MyService not being provided?

Angular
import { Component } from '@angular/core';
import { MyService } from './my.service';

@Component({
  selector: 'app-test',
  template: '<p>Test works!</p>'
})
export class TestComponent {
  constructor(private myService: MyService) {}
}
AThe component selector is missing from the module declarations.
BThe constructor parameter is missing the <code>public</code> access modifier.
CThe service <code>MyService</code> is not listed in any <code>providers</code> array or marked with <code>@Injectable({providedIn: 'root'})</code>.
DThe service file path is incorrect in the import statement.
Attempts:
2 left
💡 Hint

Check how Angular knows where to get the service instance from.