0
0
Angularframework~20 mins

Loading states and error patterns in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loading States Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Angular component display during data loading?

Consider this Angular standalone component using signals to fetch data asynchronously. What will the user see while the data is loading?

Angular
import { Component, signal, effect } from '@angular/core';
import { inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-loading-example',
  standalone: true,
  template: `
    <div *ngIf="loading()">Loading...</div>
    <div *ngIf="error()">Error occurred</div>
    <div *ngIf="!loading() && !error()">Data: {{ data() }}</div>
  `
})
export class LoadingExampleComponent {
  private http = inject(HttpClient);
  loading = signal(true);
  error = signal(false);
  data = signal('');

  constructor() {
    this.http.get<string>('https://api.example.com/data').subscribe({
      next: (res) => {
        this.data.set(res);
        this.loading.set(false);
      },
      error: () => {
        this.error.set(true);
        this.loading.set(false);
      }
    });
  }
}
AThe component shows a blank screen until data arrives.
BThe text 'Error occurred' is shown immediately and never changes.
CThe data is displayed immediately without showing 'Loading...'.
DThe text 'Loading...' is shown until data arrives, then the data is displayed.
Attempts:
2 left
💡 Hint

Look at the loading signal and how it controls the template display.

📝 Syntax
intermediate
1:30remaining
Which option causes a syntax error in Angular template for loading state?

Identify the Angular template snippet that will cause a syntax error when used to show loading and error states.

Angular
<div *ngIf="loading">Loading...</div>
<div *ngIf="error">Error occurred</div>
<div *ngIf="!loading && !error">Content loaded</div>
A<div *ngIf="loading">Loading...</div>
B<div *ngIf="error">Error occurred</div>
C<div *ngIf="!loading && !error">Content loaded</div>
D<div *ngIf="loading || error">Loading or Error</div>
Attempts:
2 left
💡 Hint

Check the syntax for combining conditions inside *ngIf.

state_output
advanced
1:30remaining
What is the final value of the error signal after this code runs?

Given this Angular component code snippet, what is the value of error() after the HTTP request fails?

Angular
import { Component, signal } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { inject } from '@angular/core';

@Component({
  selector: 'app-error-test',
  standalone: true,
  template: `<div *ngIf="error()">Error!</div>`
})
export class ErrorTestComponent {
  private http = inject(HttpClient);
  error = signal(false);

  constructor() {
    this.http.get('https://api.example.com/fail').subscribe({
      next: () => this.error.set(false),
      error: () => this.error.set(true)
    });
  }
}
Afalse
Btrue
Cundefined
Dnull
Attempts:
2 left
💡 Hint

Consider what happens in the error callback of the subscription.

🔧 Debug
advanced
2:00remaining
Why does this Angular component never show the loading message?

Review this Angular component code. The loading message never appears. What is the cause?

Angular
import { Component, signal } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { inject } from '@angular/core';

@Component({
  selector: 'app-debug-loading',
  standalone: true,
  template: `
    <div *ngIf="loading()">Loading...</div>
    <div *ngIf="!loading()">Loaded</div>
  `
})
export class DebugLoadingComponent {
  private http = inject(HttpClient);
  loading = signal(false);

  constructor() {
    this.http.get('https://api.example.com/data').subscribe(() => {
      this.loading.set(false);
    });
  }
}
AThe loading signal is never set to true, so loading message never shows.
BThe template syntax for *ngIf is incorrect and causes a silent failure.
CThe HTTP request never completes, so loading stays false.
DThe component is missing the OnInit lifecycle hook to start loading.
Attempts:
2 left
💡 Hint

Check the initial value of the loading signal and when it changes.

🧠 Conceptual
expert
2:30remaining
Which pattern best handles loading and error states in Angular with signals?

Choose the best pattern to manage loading and error states in an Angular component using signals and HTTP requests.

AUse separate signals for loading and error, set loading true before request, false after, and error true on failure.
BUse a single signal that holds 'loading', 'error', or 'success' string states and switch template based on it.
CUse only the error signal and assume loading is false until error or data arrives.
DUse a boolean loading signal and ignore error states, showing data or loading only.
Attempts:
2 left
💡 Hint

Think about clarity and explicit state management for UI feedback.