Created
March 27, 2014 16:56
-
-
Save anonymous/9812379 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> |
This file contains hidden or 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' | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment