Complete the code to make the service injectable in Angular.
import { [1] } from '@angular/core'; @Injectable() export class DataService { constructor() {} }
The @Injectable decorator marks the class as available to be injected as a service.
Complete the code to provide the service in the root injector.
@Injectable({ providedIn: '[1]' })
export class LoggerService {
log(message: string) {
console.log(message);
}
}Setting providedIn: 'root' makes the service available application-wide.
Fix the error in the service decorator to correctly provide it in any injector.
@Injectable({ providedIn: [1] })
export class ApiService {}Using providedIn: 'any' allows the service to be provided in any injector, creating a new instance in lazy loaded modules.
Fill both blanks to create a service provided only in a specific module.
@Injectable({ providedIn: [1] })
export class FeatureService {
constructor() {}
}
@NgModule({
providers: [[2]]
})
export class FeatureModule {}Setting providedIn: null disables automatic provision, so the service must be added to the module's providers array.
Fill all three blanks to create a service with a factory provider and root scope.
@Injectable({ providedIn: '[1]', useFactory: [2] })
export class ConfigService {
constructor(public config: any) {}
}
export function createConfig() {
return new ConfigService({ apiUrl: 'https://api.example.com' });
}
@NgModule({
providers: [
{ provide: ConfigService, useFactory: [3] }
]
})
export class AppModule {}The service is provided in root with a factory function createConfig used both in the decorator and module providers.