【发布时间】:2020-07-07 21:46:10
【问题描述】:
环境
- 角度 8
- AZ-向导
要求
当用户移动到某个步骤时,我们需要让第一个(或特定的)元素获得焦点。每个步骤都有自己不同的元素(输入、按钮、下拉菜单等),这些元素应该会自动聚焦,因此用户无需手动单击即可开始流程。
代码尝试
- 在元素上使用
autofocus标签。除了第一个元素之外,这与向导一样不起作用,整套步骤都是一个 DOM。 -
在每个步骤的组件中使用 ngOnViewEdit 事件和 ViewChild 来设置焦点
ngOnViewEdit() { var emailElement = (this.email.nativeElement); 如果(电子邮件元素){ emailElement.focus(); } 别的 alert("找不到邮件元素"); } 这也不起作用。还尝试使用
setTimeout调用包装方法的主体,超时从 0 到 1000 不等。 在向导的
stepEnter中尝试访问 ViewChild 并尝试聚焦。这也失败了
组件界面
`<aw-wizard-step (stepEnter)="setFocus()">
<app-cost-impact></app-cost-impact>
</aw-wizard-step>`
组件 TS
setFocus() {
var emailComponent = <AddEmailComponent><unknown>this.wizard.currentStep;
var emailElement = (<HTMLInputElement>emailComponent.email.nativeElement);
if (emailElement) {
emailElement.focus();
} else
alert("Email element not found");
}
挑战
我们需要找出某种方法来访问每个步骤的元素,并使其足够通用以处理所有步骤
【问题讨论】: