← Back to Angular, page 3

angular-0059 · Easy fresher

What are the basic accessibility practices in an Angular app, and how do you apply them in templates?

Start with semantic HTML (button, nav, label), keep keyboard access working, bind ARIA attributes with [attr.aria-*], connect every input to a label, announce dynamic changes, and move focus after navigation. ARIA fills gaps and does not replace native elements.

Full answer

Accessibility means that people using a keyboard, a screen reader, or other assistive tools can use the app. Angular does not make an app accessible by itself, so the practices belong in your templates.

  1. Use native elements. A button is focusable, reacts to Enter and Space, and is announced as a button. A div (click) is none of these. Use a for navigation and button for actions.
  2. Label every field. <label for="email"> with a matching id, or aria-label for an icon button. Placeholders are not labels.
  3. Show errors in a way assistive tools can find: role="alert" or aria-live="polite" on the message, and aria-invalid plus aria-describedby on the input.
  4. ARIA bindings use attribute binding: [attr.aria-expanded]="open()". Property binding does not work for ARIA names such as aria-label.
  5. Keep keyboard focus visible and logical. Do not remove outlines without a replacement. After a route change, move focus to the page heading or announce the new title (the router can set the page title for you).
  6. Announce dynamic updates (a toast, a result count) with a live region. The Angular CDK has LiveAnnouncer for this.
  7. Do not rely on colour only. Text contrast should meet WCAG AA (4.5:1 for normal text).
  8. Images need alt text, or alt="" when decorative.

Tools: the Angular template compiler does not check accessibility, but ESLint rules from angular-eslint (the template accessibility set) catch many mistakes. Test with a keyboard only, then with a screen reader, and run automated checks such as axe in CI.

ARIA rule: first use a native element. Add ARIA only when no native element has the meaning you need.

typescript
import { Component, signal } from '@angular/core';
import { FormControl, ReactiveFormsModule, Validators } from '@angular/forms';

@Component({
  selector: 'app-accessible-form',
  imports: [ReactiveFormsModule],
  template: `
    <h1 tabindex="-1">Join the club</h1>

    <label for="name">Full name</label>
    <input
      id="name"
      [formControl]="name"
      [attr.aria-invalid]="name.invalid && name.touched"
      [attr.aria-describedby]="name.invalid && name.touched ? 'name-error' : null"
    />
    @if (name.invalid && name.touched) {
      <p id="name-error" role="alert">Please enter your name.</p>
    }

    <button type="button" [attr.aria-expanded]="open()" aria-controls="help" (click)="open.set(!open())">
      Help
    </button>
    @if (open()) {
      <div id="help">Use your name as it appears on your ID card.</div>
    }

    <button type="button" aria-label="Close dialog">
      <span aria-hidden="true">x</span>
    </button>
  `,
})
export class AccessibleFormComponent {
  readonly name = new FormControl('', { nonNullable: true, validators: [Validators.required] });
  readonly open = signal(false);
}

Angular 20.3, strict template check passes. The error message is linked to the input with `aria-describedby`, and the toggle button reports its state with `aria-expanded`.

Key points

  • Native elements first: `button`, `a`, `label`, `nav`, headings.
  • Bind ARIA with `[attr.aria-*]`. Connect inputs to labels and errors.
  • Manage focus after navigation, and announce dynamic updates.
  • Test with the keyboard and a screen reader. Lint templates for common errors.

Common mistakes

  • Using a clickable `div` instead of a `button`.
  • Binding `[aria-label]` as a property, which does not exist. Use `[attr.aria-label]`.
  • Removing the focus outline for looks and leaving no visible focus style.

Follow-ups

  • How would you announce that a list finished loading?
  • What does `aria-describedby` do on a form field?
  • How do you handle focus when a modal dialog opens and closes?

Tips

  • Say "native first, ARIA second". It is the sentence reviewers expect.
  • Mention keyboard-only testing, which costs nothing.
  • Name `LiveAnnouncer` from the CDK for dynamic announcements.

Evaluation rubric

  • Prefers semantic HTML and labels.
  • Uses attribute binding for ARIA correctly.
  • Mentions focus management and testing.