Skip to content

Instantly share code, notes, and snippets.

@ando19721226
Created August 27, 2012 09:06
Show Gist options
  • Save ando19721226/3486810 to your computer and use it in GitHub Desktop.
Save ando19721226/3486810 to your computer and use it in GitHub Desktop.
AngularJSのfilterのメモ
<!DOCTYPE HTML>
<html lang="ja" ng-app="myApp">
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
</head>
<body ng-controller="myCtrl">
<ul>
<li ng-repeat="user in users | filter:{age:20}">
{{user.name}} : {{user.age}}
</li>
</ul>
<ul>
<li ng-repeat="user in users | filter:isTwenty">
{{user.name}} : {{user.age}}
</li>
</ul>
<ul>
<li ng-repeat="user in users | ageFilter:20">
{{user.name}} : {{user.age}}
</li>
</ul>
<script>
function myCtrl($scope) {
$scope.users = [{name: 'userA', age: 10},
{name: 'userB', age: 20},
{name: 'userC', age: 30}];
$scope.isTwenty = function(user){
return (user.age === 20);
};
};
angular.module('myApp', []).filter('ageFilter', function () {
return function (user, age) {
var i, len, out = [];
for (i = 0, len = user.length; i < len; i += 1) {
if (user[i].age === age) {
out.push(user[i]);
}
}
return out;
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment