0
0
Angularframework~20 mins

Service-based state management in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Angular service state persist across components?

Consider an Angular service that holds a counter value. If two different components inject this service and update the counter, what will be the final counter value seen by both components?

Assume the service starts with counter = 0. Component A increments it by 1, then Component B increments it by 2.

Angular
export class CounterService {
  counter = 0;
  increment(value: number) {
    this.counter += value;
  }
}

// Component A calls: counterService.increment(1);
// Component B calls: counterService.increment(2);
AComponent A sees 1, Component B sees 2.
BBoth components see counter value 0 because services are recreated.
CBoth components see counter value 3.
DComponent A sees 2, Component B sees 3.
Attempts:
2 left
💡 Hint

Think about how Angular injects services by default and whether the service instance is shared.

📝 Syntax
intermediate
1:30remaining
Identify the correct way to inject a service in Angular standalone component

Which of the following is the correct syntax to inject a service named DataService into a standalone Angular component using the new Angular 17+ inject() function?

Aconst dataService = new DataService();
Bconst dataService = inject(DataService);
Cconstructor(private dataService: DataService) {}
Dconst dataService = useInject(DataService);
Attempts:
2 left
💡 Hint

Standalone components can use the inject() function instead of constructor injection.

state_output
advanced
2:30remaining
What is the output after updating service state asynchronously?

Given this Angular service and component code, what will be logged to the console?

export class MessageService {
  message = 'Hello';
  updateMessage(newMsg: string) {
    setTimeout(() => {
      this.message = newMsg;
    }, 1000);
  }
}

@Component({
  standalone: true,
  selector: 'app-msg',
  template: `{{msgService.message}}`
})
export class MsgComponent {
  constructor(public msgService: MessageService) {}
  ngOnInit() {
    this.msgService.updateMessage('Hi');
    console.log(this.msgService.message);
  }
}
ALogs 'Hello' immediately, template updates to 'Hi' after 1 second.
BLogs 'Hi' immediately, template shows 'Hi' immediately.
CLogs 'Hello' and template never updates.
DLogs 'Hi' immediately, template updates to 'Hello' after 1 second.
Attempts:
2 left
💡 Hint

Consider when the setTimeout callback runs compared to the console.log.

🔧 Debug
advanced
2:00remaining
Why does the Angular service state reset on navigation?

An Angular service holds user data but resets to initial state when navigating between routes. What is the most likely cause?

AAngular services always reset on route changes by design.
BThe service is provided in root, so it resets on navigation.
CThe component recreates the service manually on each navigation.
DThe service is provided in a lazy-loaded module, causing multiple instances.
Attempts:
2 left
💡 Hint

Think about how Angular creates service instances with lazy-loaded modules.

🧠 Conceptual
expert
3:00remaining
What is the main benefit of using service-based state management in Angular?

Why do Angular developers often use services to manage shared state instead of component inputs or outputs?

AServices provide a single source of truth and persistent state across components.
BServices automatically update the UI without change detection.
CServices force components to reload on every state change.
DServices prevent any state changes unless the app reloads.
Attempts:
2 left
💡 Hint

Consider how shared state should behave across multiple components.