Why formatters does not work with isolated scope?

怎甘沉沦 提交于 2019-12-10 14:24:33

问题


Why formatters does not work with isolated scope? Is this angular bug or I am doing something wrong?

This contains isolates scope and does not work: http://jsfiddle.net/YbdXQ/56/

 restrict: 'A',
 scope:{},
 link: function(scope, elm, attrs, ctrl) {
      ctrl.$formatters.unshift(function(modelValue) {
          console.log("In formatters" + modelValue);
         return $filter('date')(modelValue);
     });

This does not contain isolated and scope works fine: http://jsfiddle.net/YbdXQ/57/

 restrict: 'A',
 link: function(scope, elm, attrs, ctrl) {
      ctrl.$formatters.unshift(function(modelValue) {
          console.log("In formatters" + modelValue);
         return $filter('date')(modelValue);
     });

回答1:


This doesn't have anything to do with formatters, but rather the fact that ngModel no longer has access to the value you're trying to pass it. When you're creating an isolate scope, myDate is no longer available to the ngModel directive (since you've created a new scope--an isolate scope--that doesn't have myDate on it). As proof, here's a not-so-useful example that set's myDate on the scope based on what's passed in to the ngModel attribute: http://jsfiddle.net/YbdXQ/78/

angular/angular.js#1069, "One directive's isolation scope isolates other directives on the same element," talks about this very problem:

For example, notice how my custom directive is preventing ng-model from working

You may also be interested in this StackOverflow question, "ngModel and component with isolated scope".



来源:https://stackoverflow.com/questions/14495882/why-formatters-does-not-work-with-isolated-scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!