Created
December 15, 2012 04:07
-
-
Save jrmoran/4291285 to your computer and use it in GitHub Desktop.
AngularJS filtering object and modules. view here http://jsbin.com/acagag/4/edit. (SO 13887504)
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('utils', []) | |
.factory('utils', function(){ | |
return{ | |
compareStr: function(stra, strb){ | |
stra = ("" + stra).toLowerCase(); | |
strb = ("" + strb).toLowerCase(); | |
return stra.indexOf(strb) !== -1; | |
} | |
}; | |
}); | |
angular.module('filters',['utils']) | |
.filter('friendFilter', function(utils){ | |
return function(input, query){ | |
if(!query) return input; | |
var result = []; | |
angular.forEach(input, function(friend){ | |
if(utils.compareStr(friend.name, query) || | |
utils.compareStr(friend.phone, query)) | |
result.push(friend); | |
}); | |
return result; | |
}; | |
}); | |
angular.module('app',['filters']) | |
.controller('HelloCntl', function($scope) { | |
$scope.friends = { | |
john: { | |
name: 'John', | |
phone: '555-1276' | |
}, | |
mary: { | |
name: 'Mary', | |
phone: '800-BIG-MARY' | |
}, | |
mike: { | |
name: 'Mike', | |
phone: '555-4321' | |
}, | |
adam: { | |
name: 'Adam', | |
phone: '555-5678' | |
}, | |
julie: { | |
name: 'Julie', | |
phone: '555-8765' | |
} | |
}; | |
}); |
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
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script> | |
<div ng-app='app'> | |
<div ng-controller="HelloCntl"> | |
<input placeholder="Type to filter" ng-model="query"> | |
<ul> | |
<li ng-repeat="friend in friends | friendFilter:query"> | |
<span>{{friend.name}} @ {{friend.phone}}</span> | |
</li> | |
</ul> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment