【发布时间】:2015-09-22 13:42:21
【问题描述】:
我需要在指令中检索文本输入的值,使用 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事件的第一部分。keydown是字符输入完成,然后绑定值。