Angular对话框框键盘可访问性,但未将焦点初始化在对话框框。
创始人
2024-10-24 15:31:31
0

在Angular中,要实现对话框的初始键盘焦点,可以使用Angular Material Dialog模块提供的选项。

首先,在打开对话框时,需要将焦点设置在要让用户聚焦的元素上,可以使用@ViewChild装饰器来获取模板中的DOM元素。

例如,在模板中添加一个按钮作为焦点元素:


  

然后在组件代码中获取该按钮元素,将焦点设置在它上面:

import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';

@Component({
  selector: 'dialog-example',
  templateUrl: 'dialog-example.html',
})
export class DialogExampleComponent implements AfterViewInit {
  @ViewChild('focusEl', { static: false }) focusEl: ElementRef;

  constructor(
    public dialogRef: MatDialogRef) {}

  ngAfterViewInit(): void {
    this.focusEl.nativeElement.focus();
  }
}

在这个示例中,使用了Angular Material对话框模块提供的MatDialogRef API来处理对话框的行为。

最后,当打开该对话框时,可以设置对话框选项,以便让对话框跳过其它可聚焦元素,直接将焦点设置在按钮元素上:

import { Component } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { DialogExampleComponent } from './dialog-example.component';

@Component({
  selector: 'app-root',
  template: '',
})
export class AppComponent {
  constructor(private dialog: MatDialog) {}

  openDialog(): void {
    const dialogRef = this.dialog.open(DialogExampleComponent, {
      autoFocus: false,
    });

    dialogRef.afterClosed().subscribe(() => {

相关内容

热门资讯

第5分钟辅助!hhpoker有... 第5分钟辅助!hhpoker有没有作弊挂,wepoker到底有没有透视,法门教程(竟然有挂)亲,关键...
4分钟辅助!werplan怎么... 4分钟辅助!werplan怎么透视,wepoker脚本下载,举措教程(有挂存在)1)wepoker脚...
第十分钟辅助!wepoker私... 第十分钟辅助!wepoker私人局俱乐部怎么进,wepoker透视有吗,练习教程(有挂教程)wepo...
6分钟辅助!pokerrrr2... 6分钟辅助!pokerrrr2辅助,哈糖大菠萝免费辅助器,举措教程(有挂猫腻)1)哈糖大菠萝免费辅助...
五分钟辅助!wepoker底牌... 五分钟辅助!wepoker底牌透视,淘宝买wepoker透视有用吗,教程书教程(揭秘有挂)所有人都在...
9分钟辅助!wepoker轻量... 9分钟辅助!wepoker轻量版有透视吗,wepoker祈福有用吗,总结教程(有挂规律)1、玩家可以...
七分钟辅助!wpk辅助器,we... 七分钟辅助!wpk辅助器,wepoker有辅助功能吗,方针教程(有挂详细)1、七分钟辅助!wpk辅助...
6分钟辅助!来玩app破解版,... 6分钟辅助!来玩app破解版,wepoker辅助器免费,方针教程(有挂细节)1、在wepoker辅助...
五分钟辅助!模拟器打开hhpo... 五分钟辅助!模拟器打开hhpoker,hhpoker辅助器,教程书教程(竟然有挂)1、下载好模拟器打...
六分钟辅助!来玩app破解,德... 六分钟辅助!来玩app破解,德州局脚本,方式教程(有挂教程)1、进入到德州局脚本是否有挂之后,能看到...