0
0
Angularframework~20 mins

Service-to-service injection in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
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 service injection?

Given two Angular services where ServiceB injects ServiceA, what will be logged when ServiceB.logMessage() is called?

Angular
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class ServiceA {
  message = 'Hello from ServiceA';
}

@Injectable({ providedIn: 'root' })
export class ServiceB {
  constructor(private serviceA: ServiceA) {}

  logMessage() {
    console.log(this.serviceA.message);
  }
}

// Assume ServiceB.logMessage() is called in a component
ALogs: 'undefined'
BLogs: 'Hello from ServiceA'
CThrows a runtime error: Cannot read property 'message' of undefined
DLogs: 'Hello from ServiceB'
Attempts:
2 left
💡 Hint

Remember that Angular injects the instance of ServiceA into ServiceB automatically.

lifecycle
intermediate
1:30remaining
When is the injected service instance created in Angular?

Consider two services where ServiceB injects ServiceA. When is the instance of ServiceA created by Angular?

AOnly when <code>ServiceA</code> is explicitly instantiated with <code>new</code>
BAt application startup before any component loads
CWhen <code>ServiceB</code> is instantiated for the first time
DWhen the component that uses <code>ServiceA</code> is destroyed
Attempts:
2 left
💡 Hint

Think about Angular's lazy instantiation of services provided in root.

🔧 Debug
advanced
2:30remaining
Why does this Angular service injection cause a cyclic dependency error?

Examine the following code snippet. Why does Angular throw a cyclic dependency error?

Angular
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class ServiceA {
  constructor(private serviceB: ServiceB) {}
}

@Injectable({ providedIn: 'root' })
export class ServiceB {
  constructor(private serviceA: ServiceA) {}
}
ABecause ServiceA and ServiceB inject each other directly, creating an infinite loop
BBecause Angular services cannot inject other services
CBecause the services are missing @Injectable decorators
DBecause ServiceB is not provided in any module
Attempts:
2 left
💡 Hint

Think about what happens when two services depend on each other directly.

📝 Syntax
advanced
1:30remaining
Which option correctly injects ServiceA into ServiceB in Angular?

Choose the correct syntax for injecting ServiceA into ServiceB using Angular's dependency injection.

Aconstructor(private serviceA: ServiceA) {}
Bconstructor(serviceA: ServiceA) { this.serviceA = serviceA; }
Cconstructor(private serviceA) {}
Dconstructor(@Inject(ServiceA) serviceA) { this.serviceA = serviceA; }
Attempts:
2 left
💡 Hint

Remember how Angular uses TypeScript's constructor shorthand for dependency injection.

state_output
expert
2:30remaining
What is the value of ServiceB.counter after these calls?

Given these Angular services, what is the value of ServiceB.counter after calling ServiceB.increment() twice?

Angular
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class ServiceA {
  count = 0;
  increment() {
    this.count++;
  }
}

@Injectable({ providedIn: 'root' })
export class ServiceB {
  counter = 0;
  constructor(private serviceA: ServiceA) {}

  increment() {
    this.serviceA.increment();
    this.counter = this.serviceA.count;
  }
}

// Assume ServiceB.increment() is called twice
A0
B1
CThrows an error
D2
Attempts:
2 left
💡 Hint

Track how ServiceA's count changes and how ServiceB updates its counter.