Skip to content

Instantly share code, notes, and snippets.

@mugifly
Last active September 5, 2018 17:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mugifly/4942965 to your computer and use it in GitHub Desktop.
Save mugifly/4942965 to your computer and use it in GitHub Desktop.
Angular JS little filters.
'use strict';
angular.module('foo', [])
.filter('substring', function() {
return function(str, start, end) {
return str.substring(start, end);
};
})
.filter('zpadding', function() {
return function(num, digit) {
while(num.toString().length < digit){
num = '0' + num.toString();
}
return num;
};
});
<!doctype html>
<html ng-app="foo">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
<script type="text/javascript" src="https://gist.github.com/mugifly/4942965/raw/angular-filters.js"></script>
<script type="text/javascript">
function fooCtrl($scope) {
$scope.bar = "abcdefghijklmnopqrstuvwxyz";
$scope.num = 123;
}
</script>
</head>
<body ng-controller="fooCtrl">
<h2>substring</h2>
<p> {{bar}} </p>
<p> {{bar | substring:15}} </p>
<p> {{bar | substring:0:5}} </p>
<h2>zpadding</h2>
<p> {{num}} </p>
<p> {{num | zpadding:4}} </p>
<hr>
<a href="https://gist.github.com/mugifly/4942965/">https://gist.github.com/mugifly/4942965/</a>
</body>
</html>
Copy link

ghost commented Sep 10, 2015

Thanks for the gist it helped me out

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