Skip to content

Instantly share code, notes, and snippets.

@njouanin
Last active December 29, 2017 10:24
Show Gist options
  • Save njouanin/3348ed708d527de8641c to your computer and use it in GitHub Desktop.
Save njouanin/3348ed708d527de8641c 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
@AninaRJ
Copy link

AninaRJ commented Apr 6, 2016

Is there a way to pad zeros inside an input text without using two different variables - one inside $scope and one without?

@nilsonsrjunior
Copy link

id like to know that too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment