0
0
Angularframework~10 mins

@Injectable decorator and providedIn in Angular - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the service injectable in Angular.

Angular
import { [1] } from '@angular/core';

@Injectable()
export class DataService {
  constructor() {}
}
Drag options to blanks, or click blank then click option'
ANgModule
BInjectable
CComponent
DDirective
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Injectable
Forgetting to import @Injectable
Using @NgModule or @Directive by mistake
2fill in blank
medium

Complete the code to provide the service in the root injector.

Angular
@Injectable({ providedIn: '[1]' })
export class LoggerService {
  log(message: string) {
    console.log(message);
  }
}
Drag options to blanks, or click blank then click option'
Aplatform
Bany
Croot
Dmodule
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'platform' or 'module' which are less common scopes
Forgetting to add providedIn property
3fill in blank
hard

Fix the error in the service decorator to correctly provide it in any injector.

Angular
@Injectable({ providedIn: [1] })
export class ApiService {}
Drag options to blanks, or click blank then click option'
A'root'
B'module'
C'platform'
D'any'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' when multiple instances are needed
Using an invalid string or missing quotes
4fill in blank
hard

Fill both blanks to create a service provided only in a specific module.

Angular
@Injectable({ providedIn: [1] })
export class FeatureService {
  constructor() {}
}

@NgModule({
  providers: [[2]]
})
export class FeatureModule {}
Drag options to blanks, or click blank then click option'
Anull
BFeatureService
Croot
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' or 'any' with providers array
Forgetting to add the service to providers
5fill in blank
hard

Fill all three blanks to create a service with a factory provider and root scope.

Angular
@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 {}
Drag options to blanks, or click blank then click option'
Aroot
BcreateConfig
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using different factory names in decorator and providers
Missing providedIn or using wrong scope