AngularJS 1.2.0-rc.2 VS 1.2.0元件结合 [英] AngularJS 1.2.0-rc.2 vs 1.2.0 element binding

查看:94
本文介绍了AngularJS 1.2.0-rc.2 VS 1.2.0元件结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级我的AngularJS框架从1.2.0-rc.2到1.2.0版本,并在我已不在身边想出一个办法一个奇怪的问题来了。这个问题我已经解决了被强迫输入字段火上的模糊事件,而不是按变化事件previous。在code,因为我原来使用的指令是:

I recently upgraded my AngularJS Framework from 1.2.0-rc.2 to the 1.2.0 release and came upon a strange issue that I haven't figured out a way around. The issue I had previous resolved was forcing an input field to fire on the on-blur event instead of the on-change event. The code for the directive I originally used was:

angular.module('app', []).directive('ngModelOnblur', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        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());
                });         
            });
        }
    };
});

这是刚刚通过这里找到的建议<$c$c>https://groups.google.com/forum/?fromgroups#!searchin/angular/change$20blur/angular/LH0Q1A-qTVo/eyVIjJsFZGcJ

我已经创建了两个jsFiddles,一个使用AngularJS 1.2.0-rc.2 这里,另一个使用AngularJS 1.2.0 这里

I've created two jsFiddles, one using AngularJS 1.2.0-rc.2 here, and the other using AngularJS 1.2.0 here.

ngModleOnBlur 指令应该删除改变&LT结合;输入&GT ; 元素,并添加一个明确的模糊处理结合

The ngModleOnBlur directive should remove the 'change' binding from the <input> element and add an explicit 'blur' binding.

您会发现,小提琴不同的表现,像 elm.bind绑定('模糊',函数(){...})不是实际上被正确绑定到元素,似乎 elm.unbind(输入)。解除('的keydown')。解除('变化')不工作相同的1.2.0。

You'll notice that the fiddles behave differently, like the binding for elm.bind('blur', function(){...}) isn't actually being bound properly to the element, and it seems that elm.unbind('input').unbind('keydown').unbind('change') doesn't work the same in 1.2.0.

我知道新的 NG-模糊指令的,但对我来说,我不能用直接,不过需要手动覆盖绑定到元素的事件。如果有人可以张贴的如何手动覆盖绑定到一个元素的事件,和/或解释为何这已经从1.2.0-rc.2改为1.2.0工作的jsfiddle,这将是难以置信的帮助。

I am aware of the new ng-blur directive, but in my case I can't use that directly, but need to manually override the events bound to the element. If someone could post a working jsfiddle of how to manually override the events bound to an element, and/or explain why this has changed from 1.2.0-rc.2 to 1.2.0, that would be incredibly helpful.

推荐答案

它与顺序指令正在评估做的。

It has to do with the order that the directives are being evaluated.

输入指令,它侦听DOM元素的事件,实际上是被你的指令后执行。只需添加

The input directive, which listens for the DOM element events, is actually being executed after your directive. Just add

priority: 1

要你的指令定义。输入指令为0,默认优先级。

To your directive definition. The input directive has a default priority of 0.

下面是更新小提琴

http://jsfiddle.net/yC627/

有什么奇怪的,虽然是该文件明确指出,

What is strange, though, is that the documentation explicitly states that

Directives with greater numerical priority are compiled first.

但玩弄它,它似乎是相反的。我不能告诉你这是为什么。 Hopefuly别人可以附和

but playing around with it, it seems to be the opposite. I can't tell you why that is. Hopefuly somebody else can chime in.

编辑:我看了一下更改日志,它似乎从这种变化

I looked at the change log and it seems to be from this change

https://github.com/angular/angular.js/blob/master/CHANGELOG.md#breaking-changes-1

的文档应该被更新,以反映这一点。链接功能后以相反的优先级顺序决定相比,pre-链接或编译的功能。

The documentation should really be updated to reflect this. Post-link functions are resolved in reverse priority order compared to pre-link or compile functions.

这篇关于AngularJS 1.2.0-rc.2 VS 1.2.0元件结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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