猿问

AngularJS:input [text] ngChange在值更改时触发

值更改时会触发ngChange(ngChange与经典的onChange事件不相似)。如何将经典的onChange事件与angularjs绑定在一起,只有在提交内容时才会触发?


当前绑定:


<input type="text" ng-model="name" ng-change="update()" />


慕姐4208626
浏览 1617回答 3
3回答

holdtom

这篇文章显示了一个指令示例,该指令将模型对输入的更改延迟到模糊事件触发之前。这是一个小提琴,显示了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 blurangular.module('app', []).directive('ngModelOnblur', function() {&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; restrict: 'A',&nbsp; &nbsp; &nbsp; &nbsp; require: 'ngModel',&nbsp; &nbsp; &nbsp; &nbsp; priority: 1, // needed for angular 1.2.x&nbsp; &nbsp; &nbsp; &nbsp; link: function(scope, elm, attr, ngModelCtrl) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (attr.type === 'radio' || attr.type === 'checkbox') return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elm.unbind('input').unbind('keydown').unbind('change');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elm.bind('blur', function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scope.$apply(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ngModelCtrl.$setViewValue(elm.val());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };});注意:正如@wjin在下面的注释中提到的,此功能在Angular 1.3(当前为beta)中通过受到直接支持ngModelOptions。有关更多信息,请参阅文档。

冉冉说

如果其他任何人正在寻求其他“输入”按键支持,这里是Gloppy提供的小提琴的更新。按键绑定代码:elm.bind("keydown keypress", function(event) {&nbsp; &nbsp; if (event.which === 13) {&nbsp; &nbsp; &nbsp; &nbsp; scope.$apply(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ngModelCtrl.$setViewValue(elm.val());&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }});
随时随地看视频慕课网APP

相关分类

AngularJS
我要回答