Advertisement
Intermediate Angular, Accessibility (a11y) in Angular

How do you add ARIA attributes to a custom Angular component to make it accessible?

Short answer

You bind ARIA attributes directly in the component template using Angular's attribute binding syntax, describing the role and current state of a custom element so a screen reader can correctly announce it, especially when a native HTML element does not already provide that…

Save Angular mock interview
Advertisement
AModel answer
You bind ARIA attributes directly in the component template using Angular's attribute binding syntax, describing the role and current state of a custom element so a screen reader can correctly announce it, especially when a native HTML element does not already provide that meaning on its own.
@Component({
  selector: 'app-menu-button',
  template: `
    <button [attr.aria-expanded]="isOpen" aria-label="Open navigation menu" (click)="toggle()">
      Menu
    </button>
  `
})
export class MenuButtonComponent {
  isOpen = false;
  toggle() { this.isOpen = !this.isOpen; }
}
Real-world example
A shopping app adds an aria expanded attribute to its dropdown filter button so screen reader users know whether the filter options are currently showing or hidden.
Likely follow-ups
  • When should you avoid using ARIA attributes instead of native HTML elements?
  • What is the difference between aria label and aria labelledby?
Related topics