Angular Material 垫标签可访问性

标签 angular accessibility wai-aria

我有一个带有文本输入控件的 mat-form-field。我有一个 mat-label,并且还直接在输入元素上放置了 aria-label 属性 attr.aria-label

mat-label 本身足以满足屏幕阅读器的需求吗? attr.aria-label 是必要的还是多余的?

<mat-form-field appearance="outline" floatLabel="always">
<mat-label>Username</mat-label>
<input attr.aria-label="Username" formControlName="Username" matInput>
</mat-form-field>

同样的问题也适用于垫选择控件。

<mat-form-field appearance="outline" floatLabel="always">
  <mat-label>Cars</mat-label>
  <mat-select formControlName="Car">
    <mat-option *ngFor="let car of cars" [value]="car.name">
    {{car.name}}
    </mat-option>
  </mat-select>
</mat-form-field>

最佳答案

关于[attr.aria-label][aria-label]之间差异的重要说明:

对于按钮,您可以设置动态aria-label,如下所示:

 <button [attr.aria-label]="'This is a button for ' + buttonDescription">Click me</button>

这不适用于所有 Material 控件,因为有些 Material 控件定义了 @Input('aria-label') 属性,该属性在内部设置属性(通过 @Hostbinding)

mat-select is one such control

源代码:

@Input('aria-label')
ariaLabel: string

因此,如果您尝试对 mat-select 使用 [attr.] 语法,它将不起作用,因为该控件实际上会覆盖您显式设置的属性(因为mat-selectariaLabel 输入属性永远不会被设置!)。

<mat-select [attr.aria-label]="'This will be overwritten with undefined!'">...</mat-select>

相反,您必须使用 aria-label 输入属性。

 <mat-select [aria-label]="'Choose your own adventure! ' + 123">...</mat-select>

 <mat-select aria-label="{{ 'Choose your own adventure - ' + 123 }}">...</mat-select>

 <mat-select aria-label="Choose your own adventure 123">...</mat-select>

构建会告诉您是否在应该使用 [attr.aria-label] 时使用了 [aria-label] (因为编译器会告诉您有没有这样的输入属性)。因此,如果您不想寻找您正在使用的控件的 API,请从 [aria-label] 开始。

https://material.angular.io/components/select/api

关于Angular Material 垫标签可访问性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56690162/

相关文章:

html - 如何使用 ionic 制作 pdf 生成器?

html - 当专注于某个项目时,对讲/画外音不会读取 <legend>

html - 指定 aria-label 和 title

angular - 如何用新的 observable 替换 http observable?

angular - 从 Angular 6 升级到 Angular 8 时出错

javascript - Angular 6 : Why is child component template rendered twice?

jquery - 使用 jQuery 508 是否兼容?

iOS:CALayer 的辅助功能标签

html - 从带有可访问性标签的 HTML 生成 PDF

html - 在 HTML5 中使用 ARIA 实现辅助功能的单独 tabIndex 顺序