Skip to content

Instantly share code, notes, and snippets.

@tedhagos
Created November 24, 2017 00:53
Show Gist options
  • Select an option

  • Save tedhagos/90405a635c25ab87709d4e849bd6f3c6 to your computer and use it in GitHub Desktop.

Select an option

Save tedhagos/90405a635c25ab87709d4e849bd6f3c6 to your computer and use it in GitHub Desktop.
Filter examples
let app = angular.module('app', []);
app.controller('ctrl', ($scope) => {
$scope.numbers = [0,1,2,3,4,5,6,7,8,9,10];
$scope.names = [
{name: 'James Gosling', likes: 2 },
{name: 'Misko Hevery', likes: 3 },
{name: 'Ryan Dahl', likes: 4 },
{name: 'Brendan Eich', likes:2 }
];
});
app.filter('stars', () => {
return function(num) {
let star = "";
for(let i=0; i<num; i++) {
star += '\u2605'; // The star symbol in HTML entities
}
return star;
}
});
app.factory('likesFilter', function() {
return function(num) {
let star = "";
for(let i=0; i<num; i++) {
star += '\u2606'; // The star symbol in HTML entities
}
return star;
}
});
app.filter('digitToWords', () => {
return function(num) {
let word = "";
switch(num) {
case 0:
word = "zilch";
break;
case 1:
word = "one";
break;
case 2:
word = "two";
break;
case 3:
word = "three";
break;
case 4:
word= "four";
break;
case 5:
word = "cinco";
break;
default:
word = "no idea";
}
return word;
}
});
<html ng-app="app">
<ul ng-controller="ctrl">
<li ng-repeat="n in numbers">{{n | digitToWords }}</li>
</ul>
<script src="bower_components/angular/angular.js"></script>
<script src="filter.js"></script>
</html>
<html ng-app="app">
<ul ng-controller="ctrl">
<li ng-repeat="n in names">{{n.name }} {{ n.likes | stars }}</li>
</ul>
<script src="bower_components/angular/angular.js"></script>
<script src="filter.js"></script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment