【发布时间】:2020-04-26 20:12:26
【问题描述】:
我有一个简单的角度材料模态。我想在打开/加载时关注是按钮。我确实通过在 no 按钮上设置 tabindex='-1' 并在提供程序中设置 autoFocus: true 来实现这一点。但是,这会导致用户体验问题,您无法使用键盘聚焦 no 按钮。如何在保持键盘可访问的同时聚焦第二个按钮。我还尝试使用tabindex,这似乎没有影响选择。我正在寻找一个优雅的解决方案(所以最好不要写一些打字稿来解决它)
dialog.component.html
<h2 mat-dialog-title>Cookie request</h2>
<mat-dialog-content>Should we use a cookie to automatically log you in next time?</mat-dialog-content>
<mat-dialog-actions>
<button mat-button [mat-dialog-close]='false' tabindex='-1'>No</button>
<button mat-button [mat-dialog-close]="true" tabindex='1'>Yes</button>
</mat-dialog-actions>
摘自 app.module.ts
@NgModule({
declarations: [...stuff],
imports: [...stuff],
providers: [
{provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {hasBackdrop: true, disableClose: true, autoFocus: true }}
],
entryComponents: [
DialogComponent
],
bootstrap: [AppComponent]
})
官方文档:https://material.angular.io/components/dialog/api
可选:如何强制焦点停留在我的模态框内直到它关闭?
【问题讨论】:
-
在旁注中,我在
div中使用了cdkTrapFocus,围绕着提供的片段以保持焦点在我的模态中:material.angular.io/cdk/a11y/api#CdkTrapFocus
标签: html angular angular-material-5