Complete the code to inject a service into a component.
constructor(private [1]: DataService) {}In Angular, the service is injected using a variable name with camelCase, like dataService.
Complete the code to provide a service at the root level.
@Injectable({ providedIn: '[1]' })Providing a service at the root level makes it a singleton shared across the app.
Fix the error in the service method call inside the component.
this.[1].getData().subscribe(data => { this.items = data; });The service instance variable is dataService, not the class name.
Fill both blanks to create a singleton service and inject it.
@Injectable({ providedIn: '[1]' })
export class LoggerService {
log(message: string) { console.log(message); }
}
constructor(private [2]: LoggerService) {}Providing in root makes the service singleton. The injected variable is loggerService.
Fill all three blanks to create and use a shared data service.
import { Injectable } from '@angular/core'; @Injectable({ providedIn: '[1]' }) export class SharedDataService { data = []; add(item: string) { this.data.push(item); } } @Component({ selector: 'app-list', template: `<ul><li *ngFor="let item of [2].data">{{item}}</li></ul>` }) export class ListComponent { constructor(private [3]: SharedDataService) {} }
The service is provided in root for sharing. The injected variable is sharedDataService, used in template and constructor.
