Angularjs: how to "replay" $ formatters when changing any setting?

The date format option is in the area.

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

When the format parameter in the area changes, the date displayed through the filters is updated using angular.

But not input fields via $ formatters.

The only way we forced the update was to set all the fields to zero, and then in $ timeout to reset them to the value displayed for forcing $ formatters to repeat.

Is there a better way to do this?

Answer:

By combining the answer from Sergey Moiseev and answering question 11380866, I was able to add a satisfactory solution to this directive.

Save the settings in the root directory (since it is used from the batch of directives in the allowed volume) and reacts to it using the general 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();
                }
            });

Thank!

+2
source share

All Articles