Created
April 18, 2016 13:28
-
-
Save legalt/f135aa8d20a90cd87e7d7f3e22c06315 to your computer and use it in GitHub Desktop.
AngularJS Filter for ng-repeat: filtering from first char
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
(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); | |
}); | |
}; | |
} | |
})(); |
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
<!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