【问题标题】:Angular Material mat-label accessibility角材料垫标签可访问性
【发布时间】:2019-06-20 16:17:53
【问题描述】:

我有一个带有文本输入控件的 mat-form-field。我有一个 mat-label 并且我还直接在 input 元素上放置了一个 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-select 控件。

<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>

【问题讨论】:

    标签: angular accessibility wai-aria


    【解决方案1】:

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

    对于按钮,您可以像这样设置动态aria-label

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

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

    mat-select is one such control

    源代码:

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

    因此,如果您尝试对mat-select 使用[attr.] 语法,它将无法正常工作,因为控件实际上会覆盖您明确设置的属性(因为ariaLabel 输入属性mat-select 永远不会被设置!) .

    <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

    【讨论】:

      【解决方案2】:

      如果您使用 mat 标签字段,则不应直接在输入元素上放置 aria-label。在您的情况下,它是多余的。

      如果指定了浮动标签,它将自动用作表单域控件的标签。如果未指定浮动标签,则应使用 aria-label、aria-labelledby 或

      标记表单字段控件

      [https://material.angular.io/components/form-field/overview][1]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        • 2019-07-27
        • 2020-07-05
        • 1970-01-01
        • 1970-01-01
        • 2020-11-08
        • 2020-04-20
        相关资源
        最近更新 更多