Skip to content

Instantly share code, notes, and snippets.

@legalt
Created April 18, 2016 13:28
Show Gist options
  • Save legalt/f135aa8d20a90cd87e7d7f3e22c06315 to your computer and use it in GitHub Desktop.
Save legalt/f135aa8d20a90cd87e7d7f3e22c06315 to your computer and use it in GitHub Desktop.
AngularJS Filter for ng-repeat: filtering from first char
(function(){
'use strict';
angular
.module('FilterApp', [])
.controller('filterCtr', filterCtr)
.filter('charFilter', charFilter);
function filterCtr(){
var self = this;
self.filter = '';
self.list = [
'Vasily',
'Vlad',
'Andrew',
'Sergey',
'Roman'
];
}
function charFilter(){
return function(input, text){
return input.filter(function(item){
item = item.toLowerCase();
text = text.toLowerCase();
return item.startsWith(text);
});
};
}
})();
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<script src="app.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body x-ng-app="FilterApp">
<div x-ng-controller="filterCtr as FC">
<ul>
<li x-ng-repeat="person in FC.list | charFilter: FC.filter" ng-bind='::person'></li>
</ul>
<input type="text" placeholder="Filter" x-ng-model="FC.filter"/>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment