angular 获取键盘输入的值

注意不要用event.keyCode, https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
 

js代码

resetBankAccountNumber(id) {
    this.setBankAccountValidity('ownBankAccount', true, id);
  }

  isInputtedValidKey(key) {
    if (key !== 'Tab' && key !== 'CapsLock') {
      return true;
    }
    return false;
  }

  filterValidKeyAndResetBankAccount(id, event) {
    if (event && this.isInputtedValidKey(event.key)) {
      this.resetBankAccountNumber(id);
    }
  }



  setBankAccountValidity(type, value, id) {
    if (this.form['bankAccountNumberField' + id]) {
      this.form['bankAccountNumberField' + id]['bankAccountNumber' + id].$setValidity(type, value);
    }
  }

html代码

  <input type="text"
           id="bank-account-number-input-{{ vm.debt.id }}"
           name="bankAccountNumber{{ vm.debt.id }}"
           ng-pattern="/^[0-9]+$/"
           ng-maxlength="9"
           ng-minlength="2"
           required
           ng-keyup="vm.validateBankAccountNumber(vm.debt.bankAccount.bsbCode,
                                                  vm.debt.bankAccount.bankAccountNumber,
                                                  vm.debt.id)"
           ng-keydown="vm.filterValidKeyAndResetBankAccount(vm.debt.id, $event)"
           ng-model="vm.debt.bankAccount.bankAccountNumber"
           placeholder="XXXXXXX" />

发表评论

电子邮件地址不会被公开。 必填项已用*标注