3 回答

TA貢獻1805條經驗 獲得超10個贊
這篇文章顯示了一個指令示例,該指令將模型對輸入的更改延遲到模糊事件觸發之前。
這是一個小提琴,顯示了ng-change與新的ng-model-on-blur指令一起使用。請注意,這是對原始提琴的略微調整。
如果將指令添加到代碼中,則將綁定更改為:
<input type="text" ng-model="name" ng-model-onblur ng-change="update()" />
這是指令:
// override the default input to update on blur
angular.module('app', []).directive('ngModelOnblur', function() {
return {
restrict: 'A',
require: 'ngModel',
priority: 1, // needed for angular 1.2.x
link: function(scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;
elm.unbind('input').unbind('keydown').unbind('change');
elm.bind('blur', function() {
scope.$apply(function() {
ngModelCtrl.$setViewValue(elm.val());
});
});
}
};
});
注意:正如@wjin在下面的注釋中提到的,此功能在Angular 1.3(當前為beta)中通過受到直接支持ngModelOptions。有關更多信息,請參閱文檔。

TA貢獻1877條經驗 獲得超1個贊
如果其他任何人正在尋求其他“輸入”按鍵支持,這里是Gloppy提供的小提琴的更新。
按鍵綁定代碼:
elm.bind("keydown keypress", function(event) {
if (event.which === 13) {
scope.$apply(function() {
ngModelCtrl.$setViewValue(elm.val());
});
}
});
- 3 回答
- 0 關注
- 1657 瀏覽
添加回答
舉報