2

A date format setting is in the scope.

It is used by filters to display dates in label and by a directive putting a function in $formatters from ngModel to format date in input.

When the format setting in the scope changes, the date displayed through the filters is refreshed by angular.

But not the input fields through $formatters.

The only way to force this refreshed we have found is to set all fields to null, then in $timeout to reset them to the value to display to force the reexecution of the $formatters.

Is there a better way to do this?

Answer:

By merging answer from Sergey Moiseev and answer to 11380866, I was able to add a satisfying solution to the directive.

Store the settings in rootscope (as it is used from lot of directive in solated scope) and react to it by the generic code:

$rootScope.$watch('userSettings.dateFormat.fmt', function (newVal, oldVal, scope) {
                if (newVal != oldVal) {
                    var viewValue = ngModelCtrl.$modelValue;
                    for (var i in ngModelCtrl.$formatters) {
                        viewValue = ngModelCtrl.$formatters[i](viewValue);
                    }
                    ngModelCtrl.$viewValue = viewValue;
                    ngModelCtrl.$render();
                }
            });

Thanks!

Fred
  • 521
  • 1
  • 4
  • 7
  • 1
    Can we see your code? It's pretty hard to figure out what you're doing without it. – Th0rndike Jul 02 '14 at 13:58
  • https://github.com/angular/angular.js/issues/3407 – Sergey Moiseev Jul 02 '14 at 14:45
  • 1
    Great it is that. Note that your fiddle works by adding this code to your watch:`code`var viewValue = ngModel.$modelValue; for (var i in ctrl.$formatters) { viewValue = ngModel.$formatters[i](viewValue); } ngModel.$viewValue = viewValue; ngModel.$render(); – Fred Jul 02 '14 at 15:23

0 Answers0