为什么NgModelController。$ modelValue!= $解析(attrs.ngModel)(范围)? [英] Why NgModelController.$modelValue != $parse(attrs.ngModel)(scope)?

查看:239
本文介绍了为什么NgModelController。$ modelValue!= $解析(attrs.ngModel)(范围)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code: http://plnkr.co/edit/xPZM5E7tjYqlt5NIabIu? p = preVIEW 行号:17

在此code,如果我使用 CTRL $ modelValue = NVAL; 而不是 $解析(attrs.ngModel).assign(范围,NVAL); 则不起作用。能否请您点出了原因?

  angModule.directive('moChangeProxy',函数($解析){
    返回{
        要求:'^ ngModel',
        限制:'A',
        链接:功能(范围,榆树,ATTRS,CTRL){
            VAR proxyExp = attrs.moChangeProxy;
            范围。$腕表(proxyExp,功能(NVAL){
                如果(NVAL!= Ctrl键。$ modelValue){
                    //ctrl.$modelValue = NVAL; //这不起作用
                    $解析(attrs.ngModel).assign(范围,NVAL); //这个效果很好
                }
            });
            elm.bind('模糊',函数(){
                VAR proxyVal = $范围的eval(proxyExp)。
                如果(按Ctrl。$ modelValue!= proxyVal){
                    范围。$应用(函数(){
                        $解析(proxyExp).assign(范围,CTRL $ modelValue);
                    });
                }
            });
        }
    };
});


解决方案

我想通过不工作你的意思是,当变化发生时,它并不像所有$格式化运行的东西,并更新$ viewValue?

如果是这样,这是因为ngModelController钟表模式前pression的变革,而不是看自己的$ modelValue。

请参阅这些行:<一href=\"https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1046-L1065\">https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1046-L1065

如果您更新模型,则手表被触发,所有的机器被付诸行动。如果$ modelValue的ngModelController不知道这种变化。

Code: http://plnkr.co/edit/xPZM5E7tjYqlt5NIabIu?p=preview line-no:17

In this code if I use ctrl.$modelValue = nVal; instead of $parse(attrs.ngModel).assign(scope, nVal); then it does not work. Can you please point-out the reason?

angModule.directive('moChangeProxy', function ($parse) {
    return {
        require:'^ngModel',
        restrict:'A',
        link:function (scope, elm, attrs, ctrl) {
            var proxyExp = attrs.moChangeProxy;            
            scope.$watch(proxyExp, function (nVal) {
                if (nVal != ctrl.$modelValue) {
                    //ctrl.$modelValue = nVal;  // This does not work                  
                    $parse(attrs.ngModel).assign(scope, nVal); // This works well
                }
            });
            elm.bind('blur', function () {
                var proxyVal = scope.$eval(proxyExp);
                if(ctrl.$modelValue != proxyVal) {
                    scope.$apply(function(){
                        $parse(proxyExp).assign(scope, ctrl.$modelValue);
                    });
                }
            });
        }
    };
});

解决方案

I guess that by "does not work" you mean that when the change occurs, it does not run stuff like all the $formatters and update the $viewValue?

If so, this is because ngModelController watches the "model expression" for changes rather than watching its own $modelValue.

See these lines: https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1046-L1065

If you update the model then the watch is triggered and all the machinery gets put into action. If you $modelValue, the ngModelController is not aware of the change.

这篇关于为什么NgModelController。$ modelValue!= $解析(attrs.ngModel)(范围)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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