Performance: Loading states and error patterns
This concept affects how quickly users see feedback during data fetching and error handling, impacting perceived responsiveness and visual stability.
template: `<div aria-live="polite">{{ isLoading ? 'Loading...' : data }}</div>` component: fetchData() { this.isLoading = true; this.http.get(...).subscribe(res => { this.data = res; this.isLoading = false; }); }
template: `<div *ngIf="isLoading">Loading...</div><div *ngIf="!isLoading">{{data}}</div>` component: fetchData() { this.isLoading = true; this.http.get(...).subscribe(res => { this.data = res; this.isLoading = false; }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Conditional *ngIf loading/error blocks | Adds/removes nodes | 2+ per toggle | High due to layout shifts | [X] Bad |
| Single container with text swap | Text content change only | 1 per toggle | Low, stable layout | [OK] Good |
isLoading signal is true?isLoading().<div *ngIf="isLoading()">Loading...</div> correctly calls isLoading(). <div *ngIf="isLoading">Loading...</div> treats it as a property, which is incorrect for signals. <div *ngIf="isLoading = true">Loading...</div> uses assignment inside *ngIf, which is invalid. <div *ngIf="isLoading == false">Loading...</div> checks for false, which is opposite of loading.<div *ngIf="error()" role="alert">Error: {{ error() }}</div>
<div *ngIf="isLoading()">Loading...</div>
<div *ngIf="!isLoading() && !error()">Data loaded</div>isLoading() returns false and error() returns "Network error"?export class DataComponent {
isLoading = signal(false);
error = signal(null);
fetchData() {
this.isLoading = true;
fetch('/api/data')
.then(() => this.isLoading = false)
.catch(err => this.error = err.message);
}
}this.isLoading = true which reassigns the signal variable instead of calling this.isLoading.set(true). Same for error.isLoading(), error(), and data()?<div *ngIf="isLoading()">Loading spinner...</div>
<div *ngIf="error()" role="alert">Error: {{ error() }}</div>
<div *ngIf="!isLoading() && !error()">{{ data() }}</div> correctly shows loading when isLoading() is true, error message if error() is truthy, and data only if not loading and no error. The other options mix conditions incorrectly or show wrong messages for states.Pass the test and this pattern moves unknown