javascript - Why NgModelController.$modelValue != $parse(attrs.ngModel)(scope)? - Stack Overflow

Code:line-no:17 In this code if I use ctrl.$modelValue = nVal; instead of$parse(att

Code: 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);
                    });
                }
            });
        }
    };
});

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);
                    });
                }
            });
        }
    };
});
Share Improve this question edited Jan 25, 2013 at 13:00 SunnyShah asked Jan 25, 2013 at 12:46 SunnyShahSunnyShah 30.5k30 gold badges96 silver badges139 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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./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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744947221a4602695.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信