Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why formatters does not work with isolated scope?

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);
     });
like image 322
SunnyShah Avatar asked Jan 24 '13 07:01

SunnyShah


1 Answers

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

like image 169
Michelle Tilley Avatar answered Nov 18 '22 02:11

Michelle Tilley