Consider this Angular standalone component using signals to fetch data asynchronously. What will the user see while the data is loading?
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); } }); } }
Look at the loading signal and how it controls the template display.
The component starts with loading set to true, so it shows 'Loading...'. When data arrives, loading becomes false and the data is shown. If an error occurs, 'Error occurred' is shown instead.
Identify the Angular template snippet that will cause a syntax error when used to show loading and error states.
<div *ngIf="loading">Loading...</div> <div *ngIf="error">Error occurred</div> <div *ngIf="!loading && !error">Content loaded</div>
Check the syntax for combining conditions inside *ngIf.
Angular templates do not support complex expressions with logical operators like && directly inside *ngIf. You must use a method or a signal that returns a boolean instead.
Given this Angular component code snippet, what is the value of error() after the HTTP request fails?
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) }); } }
Consider what happens in the error callback of the subscription.
The HTTP request fails, so the error callback runs and sets the error signal to true.
Review this Angular component code. The loading message never appears. What is the cause?
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); }); } }
Check the initial value of the loading signal and when it changes.
The loading signal starts as false and is never set to true, so the loading message never appears.
Choose the best pattern to manage loading and error states in an Angular component using signals and HTTP requests.
Think about clarity and explicit state management for UI feedback.
Using separate signals for loading and error states is clear and allows the UI to show precise feedback. Setting loading true before the request and false after ensures correct loading display. Setting error true on failure allows error messages.