Skip to content

Instantly share code, notes, and snippets.

@leolana
Forked from njouanin/fiddle.html
Created November 28, 2016 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leolana/93c6ef036f06fc373aeccd34e477182a to your computer and use it in GitHub Desktop.
Save leolana/93c6ef036f06fc373aeccd34e477182a to your computer and use it in GitHub Desktop.
angularJS zero padding filter
<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>
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)
};
});
name: Zero padding filter for AngularJS
description: Zero padding filter for AngularJS
authors:
- Nico
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment