Skip to main content

$parsers and $formatters in Angularjs

$parsers and $formatters are array of function defined on ngModelController. When there is change in values (view or model), the value is passed through the $parsers or $formatters pipeline to convert it according to the needs.

$parsers (View to Model):

  • $parsers is an array of function which executes when there is change in view  value ($veiwValue), usually via user input.
  • $parsers cause the conversion of view value into the form desirable to assign in model value.
  • The value is passed through each $parsers function and the return value of one function is passed to next function.
  • Last return value is passed to $validators and after validation the value is assigned to model.
  • These $parsers functions are called array order (i.e. index 0 to n).
  • $parsers are not called when there is change in model value programmatically.
  • $parsers function must return some value. If it not, causes parse error and model value is set to undefined.
Let's see the simple example to understand how $parsers works. Here is code;
HTML Code:

<div ng-app="myApp" ng-controller="myAppCtrl">
 view value = <input type="text" ng-model="v" parsers-example><br>
 model value = {{v}}
</div>
Javascript Code:

var app = angular.module('myApp',[]);

app.controller('myAppCtrl',['$scope',function($scope){
 $scope.v = '';
}]);

app.directive('parsersExample',[function(){
 return {
  require : 'ngModel',
  link : function(scope, iElement, iAttribute, ngModelController){

   function parser1(veiwvalue){
    if(veiwvalue){
     return veiwvalue + '_parser1';
    }
   }

   function parser2(veiwvalue){
    if(veiwvalue){
     return veiwvalue + '_parser2';
    }
   }

   ngModelController.$parsers.push(parser2);
   ngModelController.$parsers.push(parser1);
  }
 };
}]);
In the above HTML code, we have used input field to change the view value and one way binding to see the model value. In javascript code, we have defined a directive called parsersExample. In the linking function of the directive, we have defined two parsers functions namely parsers1() and parsers2(). And then pushed the function into the $parsers array defined on ngModelController. The order of pushing (note that functions are pushed in array) of functions matters. See the working example to understand the execution of functions:



$formatters (Model to View):

  • $formatters is an array of function which execute when there is change in expression bound in ngModel programmatically, usually via events or function call.
  • $formatters cause the conversion of $modelValue into the form desirable to assign in $veiwValue. Formatters are used to format / convert the $modelValue for display.
  • The value is passed through each $formatters function and the return value of one function is passed to next function.
  • Last return value is used as actual DOM value.
  • These $parsers functions are called reverse array order (i.e. index n to 0).
  • $formatters are not called when there is change in value via user interaction.
  • $formatters function must return some value. If it not, causes error and view value is set to undefined.
Let's see the simple example to understand how $formatters works. Here is code;
HTML Code:

<div ng-app="myApp" ng-controller="myAppCtrl">
 model value = {{v}} | <button ng-click="addTwo()">Add 2 to model value</button><br><br>
 view value = <input type="text" ng-model="v" formatters-example><br>
</div>
Javascript Code:

var app = angular.module('myApp',[]);

app.controller('myAppCtrl',['$scope',function($scope){
 $scope.v = 0;
 $scope.addTwo = function(){
  $scope.v += 2;
 }
}]);

app.directive('formattersExample',[function(){
 return {
  require : 'ngModel',
  link : function(scope, iElement, iAttribute, ngModelController){

   function formatters1(modelvalue){
    if(modelvalue){
     return modelvalue + '_formatters1';
    }
   }

   function formatters2(modelvalue){
    if(modelvalue){
     return modelvalue + '_formatters2';
    }
   }

   ngModelController.$formatters.unshift(formatters2);
   ngModelController.$formatters.unshift(formatters1);
  }
 };
}]);
In the above HTML code, we have used input field to the view value and one way binding to see the model value. Function addTwo() is defined to add 2 in model value and is called via button click. In javascript code, we have defined a directive called formattersExample. In the linking function of the directive, we have defined two formatters functions namely formatters1() and formatters2(). And then unshift the function into the $formatters array defined on ngModelController. The order of unshifting (note that functions are unshift in array) of functions matters. See the working example to understand the execution of functions:


Comments

Popular posts from this blog

Automatic Slashing the Date Input using Angularjs

We define a directive that will automatically slash the input field as user typein the date. The directive have following capabiltiy: The directive will append slash '/' when input field has 2 or 5 characters. The input will accept only 10 characters (dd/mm/yyyy). Validity of date provide will not be checked. When the backspace is pressed it will remove the last two character if last character is slash. Otherwise it will remove last character from the input field. The input will not accept characters other than numbers (0 to 9). Let's jump into the code. We will use bootstrap for awesome look: HTML Code: <div class="container" ng-app="myApp" ng-controller="myAppCtrl"> <div class="row"> <div class="col-sm-8 col-sm-offset-2"> <form class="form-horizontal"> <h4 class="bg-primary">Auto Slashing Date using Angularjs Directive</h4> <div class=...

Auto generating the year in select using ngOption

While creating an angular form, we come upon a situation where we required to generate the sequence of years in the select HTML tag. There is some way to perform this task, but each has its limitation. Let's look the way to generate the years : Hard code an array having years in your controller and assign it to some $scope variable. Use for loop to generate the years and then assign to $scope variable. Send $http request to generate the array of year and assign the response data to $scope variable. Define a filter that will be used to generate the sequence of years. As the first and second method makes our controller dirty and the third method causes HTTP request that will overhead server, so we are going to see the fourth method to generate the years as it has several advantages over other three methods. So let's jump into the code: HTML Code: <form name="myform"> <select ng-options="year for year in [] | range: '2000':'20...