How do you make screen readers announce 'Filters applied' with the Angular CDK's `LiveAnnouncer`, and what do its politeness and duration arguments change?
answer
- one shared hidden region
- inject the service, call announce
- polite unless told otherwise
- a short delay before the text
- duration clears the message
basics
~20 sInject LiveAnnouncer and call announce('Filters applied'). It writes the text into one shared, visually hidden aria-live element; politeness defaults to polite, assertive interrupts, and duration clears the text that many milliseconds after it is written.
solid answer
~40 s`LiveAnnouncer` from `@angular/cdk/a11y` is a root service that owns one visually hidden live element appended to `body`, with `aria-live` and `aria-atomic="true"`. `inject(LiveAnnouncer).announce('Filters applied')` clears that element, then writes the text after about 100 ms, which lets identical repeated messages be read again. The optional `politeness` argument (`'polite'`, `'assertive'` or `'off'`) sets the element's `aria-live`; it defaults to `'polite'`, or to `LIVE_ANNOUNCER_DEFAULT_OPTIONS` when provided. `duration` clears the text that many milliseconds after it is written, so a stale message is not re-read while the user browses. `announce` returns a promise that resolves once the text is in the DOM, and a newer call cancels a pending one.
code
ts · 16 linesimport {Component, inject, signal} from '@angular/core';
import {LiveAnnouncer} from '@angular/cdk/a11y';
@Component({
selector: 'app-filter-drawer',
template: `<button type="button" (click)="apply()">Apply filters</button>`,
})
export class FilterDrawer {
private announcer = inject(LiveAnnouncer);
readonly active = signal<string[]>([]);
apply(): void {
const count = this.active().length;
this.announcer.announce(`Filters applied: ${count} active`, 'polite', 5000);
}
}go deeper
Recall that you inject LiveAnnouncer and call announce with a short message, and that polite is the default.
Walk through what announce does: clear, set aria-live, wait about 100 ms, write the text, optionally clear after duration; explain why that makes repeats audible.
Talk about announcing outcomes once instead of every change, the aria-modal workaround, app-wide defaults through LIVE_ANNOUNCER_DEFAULT_OPTIONS, and testing the asynchronous write.
Weigh imperative announce calls against cdkAriaLive on visible text, and how a team keeps announcement wording consistent across features.
## The problem the service solves A **live region** is an element that assistive technology watches: when its text changes, a screen reader speaks the new text without the user moving focus. Live regions are fragile in practice. The region usually has to exist before the text changes, and some browser and screen-reader pairs ignore a change made too quickly or skip a message identical to the previous one. The Angular CDK's `LiveAnnouncer`, from `@angular/cdk/a11y`, packages a working answer to that. It is a root-level service; the first time it is injected it creates **one** element for the whole app. ## What the service puts in the page - A `div` appended to `document.body` with the classes `cdk-live-announcer-element` and `cdk-visually-hidden`, so sighted users never see it. - `aria-atomic="true"`, so the whole message is read rather than only the changed part. - An initial `aria-live="polite"`, rewritten on every call to the politeness you pass. - A generated id, used to keep the element reachable from CDK modals (below). You can supply your own element instead through the `LIVE_ANNOUNCER_ELEMENT_TOKEN` injection token. ## What `announce` does, step by step 1. Works out the arguments: `announce(message)`, `announce(message, politeness)`, `announce(message, duration)` or `announce(message, politeness, duration)`. 2. **Clears** the element's text and cancels any announcement still waiting to be written. 3. Sets `aria-live` to the requested politeness, falling back to `LIVE_ANNOUNCER_DEFAULT_OPTIONS.politeness`, then to `'polite'`. 4. Adds the element's id to the `aria-owns` of any `aria-modal="true"` element inside the CDK overlay container, because some browsers hide content outside an open modal from assistive technology. 5. After a **100 ms** timeout, writes the message as the element's text. 6. If a `duration` was given (or a default `duration` exists in `LIVE_ANNOUNCER_DEFAULT_OPTIONS`), schedules `clear()` that many milliseconds later. 7. Resolves the returned promise once the text has been written. The clear-then-delay sequence is the important part: because the old text is removed first and the new text arrives after a pause, saying `'Filters applied'` twice in a row is still spoken twice. ## The arguments compared | Argument | Values | Default | Effect | |---|---|---|---| | `message` | a string (or sanitized HTML) | required | the text that is spoken | | `politeness` | `'polite'`, `'assertive'`, `'off'` | `'polite'` | sets the element's `aria-live` | | `duration` | milliseconds | none, the text stays | clears the text after it is written | `'polite'` waits for the screen reader to finish what it is saying; `'assertive'` may interrupt it, so it suits errors rather than confirmations like a filter count. `duration` matters because a message left in the region can be found again by a user browsing the page, which is noise once the moment has passed. Calling `clear()` yourself does the same thing. ## Announcing a filter change In a filter drawer, the Apply handler updates the results and then calls `announce`. If several calls arrive in quick succession, each cancels the previous pending write, so only the last one is spoken. The pattern is to announce the outcome once, after the work is done, rather than on every intermediate change. ## The declarative alternative: `cdkAriaLive` When the text already lives in your template, the `cdkAriaLive` directive watches an element's text content and forwards each change to `LiveAnnouncer`: - `cdkAriaLive="polite"` or `"assertive"` sets the politeness; `"off"` stops watching. - `cdkAriaLiveDuration` passes a duration. - The element stays visible; only the shared announcer element is hidden. This keeps a visible results summary and its spoken form in sync without imperative calls. ## Testing note Because the text is written after a timeout, a unit test that asserts on the announcer element right after calling `announce` sees it empty. Await the returned promise, or advance fake timers, before checking.
- Why does a test that checks the announcer element right after calling announce find it empty?`announce` clears the element and writes the new text inside a 100 ms timeout, so the text is not there synchronously. The method returns a promise that resolves once the text is in the DOM; await it, or advance fake timers past the delay, before asserting.
- How does an announcement reach screen-reader users while an Angular Material dialog with aria-modal is open?Some browsers expose only the contents of an open `aria-modal` element. On each call, `LiveAnnouncer` finds `aria-modal="true"` elements inside the CDK overlay container and adds its own element's id to their `aria-owns`, so the announcer becomes part of the modal's accessibility subtree.
- How do you change the default politeness or duration for every announcement in an app?Provide `LIVE_ANNOUNCER_DEFAULT_OPTIONS` with `{politeness, duration}`. `announce` uses those values whenever a call omits the argument, falling back to `'polite'` and no automatic clearing when the token is absent.
- Each component should create its own aria-live element for LiveAnnouncer to use.
- LiveAnnouncer defaults to assertive so the message is never missed.
- The duration argument delays the announcement by that many milliseconds.
- Announcing the same message twice is only spoken once by LiveAnnouncer.
- announce writes the text synchronously, so a test can assert on it at once.