注意不要用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" />
02-10 01:19