Keydown 不能正确刷新输入文本中的 ng-model 但 keyup 可以

Keydown doesn't properly refresh ng-model in input text but keyup does

我需要在指令中检索文本输入的值,使用 ng-model 将值发送到数据库。 如果我在我的指令中使用 keydown 事件,并在我的输入中写入例如 1234,我的指令中显示的结果是 123,如果我写 abc,结果是 ab。 如果我使用 keyup,则不存在此问题(使用 firefox 和 chrome 测试)。 为什么会发生?

我的代码:

.directive('updateDbb', ["$http", function ($http) {
 return {
 restrict: 'A',
 require: 'ngModel',
  link: function (scope, element, attrs, ngModel) {
        // Listen for change events to enable binding
        var ngModelValue, inputName, testValid, dbbFieldName, idDbb;
        element.bind('keydown', function () {
            ngModelValue = ngModel.$viewValue;
            console.log(ngModelValue); // Show with delay, not the case with "keyup"!!!

...

keydown 在用户按下某个键时触发,并且 字符被 插入 到输入中。 这就是它没有更新的原因

keypress 在您的输入中插入实际字符时触发。我认为这是您要使用的事件(如果用户按住该键,则该事件将针对每个插入的字符触发一次)

keyup 在字符插入输入后,当用户松开按键时触发。 (如果用户一直按住按键,事件只会触发一次)