← Back to Angular, page 10

angular-0184 · Hard senior

Design the front end of a dashboard with many widgets (charts, tables, KPIs) that load data independently. How do you structure the Angular app?

Treat each widget as a self-contained component with its own data resource, loading, error and empty states, so one failure does not break the page. Load above-the-fold widgets first and the rest with @defer, share data through a small service layer with caching, and keep layout configuration separate from widget code.

Full answer

Start by clarifying requirements: how many widgets, how fresh the data must be (real time or every minute), who configures the layout, what devices, and what the slowest backend call is. Then explain the design in layers.

  1. Layout. A grid component renders widgets from a configuration array ({ id, type, size, position }). Widgets are looked up by type in a registry (a map from type to component), and created with NgComponentOutlet or @switch. This allows user-defined layouts, saved per user, without changing widget code.
  2. Widget contract. Every widget is a standalone component with a small input interface (config, filters) and the same states: loading (skeleton), error (message and retry button), empty, and data. A shared wrapper component (widget-card) gives the same frame, title, refresh button and error handling.
  3. Data access. Each widget owns its request using resource() or httpResource() keyed by the global filters (date range, class). When filters change, only affected widgets reload, and in-flight requests are cancelled. A thin data service per domain (MarksApi, AttendanceApi) hides URLs, mapping and caching. For shared data (the same query in two widgets), add a short-lived cache with request deduplication, or use a batched endpoint.
  4. Loading strategy. Render the shell first. Load above-the-fold widgets right away, and everything else lazily with @defer (on viewport), with a placeholder of the same size so the layout does not jump. Charting libraries are large, so import them inside the deferred widget only.
  5. Failure isolation. Errors are handled inside the widget. A failed chart shows "Could not load. Retry" while others stay usable. Add timeouts and limited retries with backoff.
  6. Freshness. Polling with timer and switchMap (pause when the tab is hidden), or a WebSocket or server sent events for live widgets, with one shared connection in a service and share() to fan out.
  7. State. Global filters live in a signal store or a service. Widget state stays local. Put URL state (filters, tab) into query params so a link can be shared.
  8. Performance. OnPush and signals everywhere, track in lists, virtual scroll for big tables, canvas based charts for large series, and a performance budget. Measure with the profiler.
  9. Quality. Accessibility (charts need text alternatives or a data table), tests for the wrapper and for each widget's states, and error and timing logging per widget.

Name the trade-offs: independent requests are simple and isolated, but many requests; a combined endpoint is efficient, but couples widgets. A good answer mentions both and picks one for the stated numbers.

typescript
import { Component, Type, input } from '@angular/core';
import { NgComponentOutlet } from '@angular/common';

export interface WidgetConfig {
  id: string;
  type: 'marks-chart' | 'attendance-kpi';
  columns: number;
}

@Component({ selector: 'app-marks-chart', template: `Marks chart` })
export class MarksChartComponent {}

@Component({ selector: 'app-attendance-kpi', template: `Attendance KPI` })
export class AttendanceKpiComponent {}

// The registry maps a type from saved configuration to a component.
export const widgetRegistry: Record<WidgetConfig['type'], Type<unknown>> = {
  'marks-chart': MarksChartComponent,
  'attendance-kpi': AttendanceKpiComponent,
};

@Component({
  selector: 'app-dashboard-grid',
  imports: [NgComponentOutlet],
  template: `
    @for (w of widgets(); track w.id) {
      <section [style.grid-column]="'span ' + w.columns" [attr.aria-label]="w.type">
        <ng-container *ngComponentOutlet="registry[w.type]" />
      </section>
    }
  `,
  styles: [`:host { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; }`],
})
export class DashboardGridComponent {
  readonly widgets = input.required<WidgetConfig[]>();
  readonly registry = widgetRegistry;
}

Angular 20.3, strict template check passes. This is a design answer. The code shows the widget wrapper contract and the type registry, and was type-checked, but not run as a whole dashboard.

Key points

  • Widgets are self-contained components with a shared card wrapper and the same four states.
  • A layout config plus a type registry allows user-defined dashboards.
  • Each widget loads its own data (`resource`), so failures are isolated and requests cancel on filter change.
  • Use `@defer` for below-the-fold widgets, placeholders to avoid layout jumps, and shared services for caching and live data.

Common mistakes

  • One big component that loads everything, so one slow call blocks the page.
  • Starting with technology and not with requirements.
  • Charts without text alternatives.

Follow-ups

  • How would you let users add and reorder widgets?
  • How do you avoid ten widgets making the same request?
  • How would you keep a live widget from running in a hidden tab?

Tips

  • Ask clarifying questions first.
  • Name the trade-off between many requests and one combined request.
  • Cover failure, loading, performance and accessibility.

Evaluation rubric

  • Structures the solution into clear layers.
  • Handles loading, errors, freshness and performance.
  • Explains trade-offs and decisions.