【问题标题】:Angular:How to set aria-selected for option in select control for AngularAngular:如何在 Angular 的选择控件中为选项设置 aria-selected
【发布时间】:2018-11-05 19:12:34
【问题描述】:

我在 ngFor 中有一个带有多选 true 和选项元素的选择控件。现在我想将 aria-selected 设置为 true 如果选项被选择为 false 。

【问题讨论】:

  • 您可以使用 [attr.aria-selected]="selected" 其中 selected 是布尔值
  • 我尝试过这种方式,但由于它在循环中,因此该值正在更新为所有选项。
  • 您可以编辑您的问题并添加您正在使用的代码吗?

标签: html angular assistive-technology


【解决方案1】:

一个可能的解决方案是使用循环检查每个选项是否是选定选项的一部分,如果是,则使用角度 [attr.attribute-name] 模板语法将 aria-selected 设置为 true。

这是一个 simple Stackblitz example 演示此解决方案。
选择选项并查看元素是否正确后缀(意味着 aria-selected 设置为 true)。

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <p>Pick values and see that aria-selected is set to true</p>
    <select [(ngModel)]="selectedValues" multiple>
      <option *ngFor="let value of values" [ngValue]="value" [attr.aria-selected]="selectedValues.indexOf(value) >= 0">{{value}}</option>
    </select>
  `,
  styles: [
    `
      /* use CSS to add postfix to elements with aria-selected set to true */
      option[aria-selected="true"]::after {
        content : ' aria-selected';
      }
      `,
  ],
})
export class AppComponent {
  values = [
    'value 1',
    'value 2',
    'value 3',
  ];
  selectedValues = [];

  constructor() {
  }
}

【讨论】:

    猜你喜欢
    • 2020-07-19
    • 2019-01-17
    • 2019-04-28
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2021-05-19
    相关资源
    最近更新 更多