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);
});
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 thengModel
directive (since you've created a new scope--an isolate scope--that doesn't havemyDate
on it). As proof, here's a not-so-useful example that set'smyDate
on the scope based on what's passed in to thengModel
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:
You may also be interested in this StackOverflow question, "ngModel and component with isolated scope".