Last active
December 29, 2017 10:24
-
-
Save njouanin/3348ed708d527de8641c to your computer and use it in GitHub Desktop.
angularJS zero padding filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Is there a way to pad zeros inside an input text without using two different variables - one inside $scope and one without?