如何专注于PrimeNG p按钮

问题描述

我在Angular中有一个表单。按下Enter键后,我想重点关注下一个控件。在每个输入的keyup.enter事件中,我使用下一个输入的ref并调用focus()。但这不适用于p-button

<div class="p-field p-col-12 p-md-6">
  <label>Address Line 1</label>
  <input #regAddressLine1 type="text" pInputText [readonly]="!editMode" formControlName="regAddressLine1" (keyup.enter)="regAddressLine2.focus()">
  <small class="p-invalid" *ngIf="f.regAddressLine1.invalid && f.regAddressLine1.touched">required</small>
</div>
<div class="p-field p-col-12 p-md-6">
  <label>Address Line 2</label>
  <input #regAddressLine2 type="text" pInputText [readonly]="!editMode" formControlName="regAddressLine2" (keyup.enter)="save.focus()">
  <small class="p-invalid" *ngIf="f.regAddressLine2.invalid && f.regAddressLine2.touched">required</small>
</div>
<div class="p-col-12 p-md-3">
  <p-button #save label="Save" styleClass="p-button-primary">
  </p-button>
</div>

解决方法

看起来像p-button组件没有这种方法。

我会考虑使用primeng按钮的属性版本:

<button #save pButton label="Save" styleClass="p-button-primary" type="button"></button>
              ^^^^^^^^

通过这种方式,#save模板变量引用具有focus()方法的本机HTMLButtonElement。

Forked Stackblitz