0
0
Angularframework~20 mins

Injecting services into components in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Service Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular component with injected service?

Consider this Angular standalone component that injects a service to get a greeting message. What will be displayed when this component renders?

Angular
import { Component, inject } from '@angular/core';
import { GreetingService } from './greeting.service';

@Component({
  selector: 'app-greet',
  standalone: true,
  template: `<p>{{ message }}</p>`
})
export class GreetComponent {
  private greetingService = inject(GreetingService);
  message = this.greetingService.getGreeting();
}

// GreetingService code:
// export class GreetingService {
//   getGreeting() { return 'Hello, Angular!'; }
// }
AHello, World!
BHello, Angular!
CError: No provider for GreetingService
Dundefined
Attempts:
2 left
💡 Hint

Think about what the service method returns and how the component uses it.

📝 Syntax
intermediate
1:30remaining
Which option correctly injects a service in an Angular standalone component?

Choose the correct way to inject DataService into a standalone Angular component using the new inject() function.

Aprivate dataService = inject(DataService);
Bconstructor(private dataService: DataService) {}
Cinject(DataService) private dataService;
Dconst dataService = new DataService();
Attempts:
2 left
💡 Hint

Remember that inject() is used as a function call inside the class body, not in constructor parameters.

🔧 Debug
advanced
2:30remaining
Why does this Angular component fail with 'NullInjectorError'?

This component tries to inject UserService but throws NullInjectorError: No provider for UserService. What is the cause?

Angular
import { Component, inject } from '@angular/core';
import { UserService } from './user.service';

@Component({
  selector: 'app-user',
  standalone: true,
  template: `<p>User: {{ userName }}</p>`
})
export class UserComponent {
  private userService = inject(UserService);
  userName = this.userService.getUserName();
}

// UserService is NOT provided in any module or component providers array.
AThe template syntax {{ userName }} is invalid.
BThe inject() function is used incorrectly; it must be called in constructor.
CThe component is missing the @Injectable decorator on UserService.
DUserService is not registered as a provider anywhere in the app.
Attempts:
2 left
💡 Hint

Think about Angular's dependency injection system and where services must be registered.

state_output
advanced
2:00remaining
What is the value of count after clicking the button twice?

This Angular component injects a CounterService that tracks a count. What will be the displayed count after clicking the button two times?

Angular
import { Component, inject } from '@angular/core';
import { CounterService } from './counter.service';

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `<button (click)="increment()">Increment</button><p>Count: {{ count }}</p>`
})
export class CounterComponent {
  private counterService = inject(CounterService);
  count = this.counterService.getCount();

  increment() {
    this.counterService.increment();
    this.count = this.counterService.getCount();
  }
}

// CounterService code:
// export class CounterService {
//   private count = 0;
//   increment() { this.count++; }
//   getCount() { return this.count; }
// }
A2
B0
C1
DError: count is undefined
Attempts:
2 left
💡 Hint

Each click calls increment() which increases the count by 1.

🧠 Conceptual
expert
2:30remaining
Which statement about Angular service injection is TRUE?

Choose the correct statement about injecting services into Angular standalone components.

AServices must always be provided in NgModules to be injectable.
BThe inject() function can only be used inside constructors.
CStandalone components can inject services provided in their own providers array or globally.
DInjecting services requires manually creating instances with <code>new</code> keyword.
Attempts:
2 left
💡 Hint

Think about Angular's new standalone component features and service providers.