在Angular6“或”中选择1个事件

时间:2019-08-13 09:47:18

标签: angular

我想在用户按下Enter时发送表单,但是我的输入失去焦点,但是只有1次,因为如果我确实输入然后更改焦点,我的事件将执行2次(1次输入Enter,2次更改焦点) )。

  <form class="form-custom" #m="ngForm">
          <div class="form-custom--input-btn">
            <input #ofi="ngModel" type="text" class="clr-col-12 form-custom--input form-custom--input"
              id="oficina" (keyup.enter)="hello()" (focusout)="hello()" name="oficina" [(ngModel)]="oficina" minlength="4" maxlength="4" [placeholder]="placeholderOficina" required>
            <button type="button"  class="btn--custom-over-input btn--custom-over-input--arrow" [disabled]="mult.invalid" (click)="hello()"></button>
          </div>
        </form>

我的输入内容中-> (keyup.enter)="hello()" (focusout)="hello()" 然后在我的按钮中(click)=“ hello()”>

我需要Angular仅获得1个事件或(keyup.enter)="hello()" (focusout)="hello()"

1 个答案:

答案 0 :(得分:0)

首先,以表单即

的形式调用Submit总是一个好主意。
<form (submit)="doSomething()">

因此用户实际上可以按Enter键并提交表单。

第二,您可以引入一个局部变量,例如formSubmitted(是或否),该变量在您的hello方法中进行检查,如下所示:

hello() {
  if (formSubmitted) {
    return;
  }
  // doWhatEverYouNeedtoHere
}