How to manually rerun formatter chain in angularjs directive with ngModel?

Angular.js ngModel has the ability to declare a chain of parsers and formatters. Some more details can be found at the great answer to 'How to do two-way filtering in angular.js?'

now the formatter chain only will be run if the ngModel will update. so if you have a second input-parameter that affects the viewValue (is used in one of the formatters) this will not trigger an update of the View. similar as far as i found ngModel only uses a simple $watch - so if your model is a collection/object it will not trigger if sub-elements are changed.

What is the best way to implement a deep watch for ngModel -
or a watch for a additional parameter that should rerun the formatter chain?

there are other similar questions:
Angularjs: how to “rerun” $formatters when some setting is changed?



currently there is no direct api to call the internal formatter chain. there is a github feature request for this. as work-around you just can copy the internal code:

function runFormatters(ctrl){
    // this function is a copy of the internal formatter running code.

    var modelValue = ctrl.$modelValue;

    var formatters = ctrl.$formatters;
    var idx = formatters.length;

    var viewValue = modelValue;

    while (idx--) {
        viewValue = formatters[idx](viewValue);

    if (ctrl.$viewValue !== viewValue) {
        ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;

        ctrl.$$runValidators(modelValue, viewValue, angular.noop);


this Plunker demonstrates the usage in combination with a watch for additional parameters:

// deepwatch all listed attributes
        return [scope.extraThingToWatchFor, scope.someOther];
    function() {
        console.log("\t runformatters()");

this is a second Plunker to demonstrate the deepwatch on ngModel

// deepwatch ngModel
        return ngModelCtrl.$modelValue;
    function(newData) {


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.