【问题标题】:Choose 1 event in Angular6 "or"在 Angular6 中选择 1 个事件“或”
【发布时间】:2019-08-13 09:47:18
【问题描述】:

我想在用户按下输入时发送我的表单我的输入失去焦点,但只有 1 次,因为如果我输入然后我改变焦点,我的事件将执行 2 次(1 用于输入,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()"

【问题讨论】:

    标签: angular


    【解决方案1】:

    首先,在表单中调用提交总是一个好主意,即

    <form (submit)="doSomething()">
    

    所以用户实际上可以按 Enter 并提交表单。

    其次,您可以引入一个局部变量,例如 formSubmitted(它是 true 或 false),它在您的 hello 方法中进行检查,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多