我需要在指令中使用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"!!!
Run Code Online (Sandbox Code Playgroud)
...