iegorov
5/16/2014 - 8:41 PM

angularJS zero padding filter

angularJS zero padding filter

name: Zero padding filter for AngularJS
description: Zero padding filter for AngularJS
authors:
- Nico
angular.module('MyApp', ['filters']);

angular.module('filters', []).filter('zpad', function() {
	return function(input, n) {
		if(input === undefined)
			input = ""
		if(input.length >= n)
			return input
		var zeros = "0".repeat(n);
		return (zeros + input).slice(-1 * n)
	};
});
<div ng-app="MyApp">
  <h1>Zero padding filter</h1>
  <div>
    <input type="text" ng-model="ztext" placeholder="Type some text ..." /> 
  </div>
  <div>
    <h2>Result:</h2>
    <p>{{ztext | zpad:4}}</p>
  </div
</div>