Angularjs:输入[文] ngChange火灾而值正在改变 [英] Angularjs: input[text] ngChange fires while the value is changing

查看:103
本文介绍了Angularjs:输入[文] ngChange火灾而值正在改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ngChange被烧成而值正在改变(ngChange不类同经典onChange事件)。我怎样才能绑定angularjs经典onChange事件,这只会火的时候,内容上犯下?

当前绑定:

 <输入类型=文本NG模型=名NG变化=更新()/>


解决方案

这篇文章显示,延迟到输入模型的变化,直到模糊事件触发指令的例子。

这里是显示NG-变化与新的NG-模型的模糊指令工作的小提琴。请注意,这是点小文章的原来的小提琴

如果你的指令添加到您的code你会改变你的绑定到这一点:

 <输入类型=文本NG模型=名NG-模型的onblur NG-变化=更新()/>

下面是指令:

  //覆盖默认输入对模糊更新
angular.module('应用',[])。指令(ngModelOnblur',函数(){
    返回{
        限制:'A',
        要求:'ngModel',
        优先级:1,//需要的角度1.2.x的
        链接:功能(范围,榆树,ATTR,ngModelCtrl){
            如果(attr.type ==='无线电'|| attr.type ===复选框')回报;            。elm.unbind(输入)解除('的keydown')解除(变化)。
            elm.bind('模糊',函数(){
                范围。$应用(函数(){
                    ngModelCtrl $ setViewValue(elm.val())。
                });
            });
        }
    };
});


注意:如@wjin提到下面这个功能的评论是直接在角1.3通过 ngModelOptions 支持(目前处于测试阶段)。请参见的文档获得更多信息。

ngChange is firing while the value is changing (ngChange are not similiar to the classic onChange event). How can i bind the classic onChange event with angularjs, that will only fire when the contents are commited?

Current binding:

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

解决方案

This post shows an example of a directive that delays the model changes to an input until the blur event fires.

Here is a fiddle that shows the ng-change working with the new ng-model-on-blur directive. Note this is a slight tweak to the original fiddle.

If you add the directive to your code you would change your binding to this:

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

Here is the directive:

// 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());
                });         
            });
        }
    };
});


Note: as @wjin mentions in the comments below this feature is supported directly in Angular 1.3 (currently in beta) via ngModelOptions. See the docs for more info.

这篇关于Angularjs:输入[文] ngChange火灾而值正在改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆