JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.1/angular.min.js"></script>
<div data-ng-app='App'>
<div data-ng-controller="MyCtrl">
<form>
<label for="searchText">Search Text:</label>
<input data-ng-model="searchText" name="searchText" />
<br/>
<input type="checkbox" data-ng-model="throttle"> Throttle
<br/>
<label>You typed:</label> <span>{{searchText}}</span>
</form>
<ul><li data-ng-repeat="item in filteredList">{{item}}</li></ul>
</div>
</div>
JavaScript
var app = angular.module('App', []);
var randomString = function (length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
};
var filterAction = function($scope) {
if (_.isEmpty($scope.searchText)) {
$scope.filteredList = $scope.list;
return;
}
var searchText = $scope.searchText.toLowerCase();
$scope.filteredList = _.filter($scope.list, function(item) {
return item.indexOf(searchText) !== -1;
});
};
var filterDelayed = function($scope) {
$scope.$apply(function(){filterAction($scope);});
};
var filterThrottled = _.debounce(filterDelayed, 1000);
var pickFilter = function($scope) {
if ($scope.throttle) { filterThrottled($scope); } else { filterAction($scope); }
};
var controller = function ($scope) {
var list = [];
for (var idx = 0; idx < 100; idx += 1) {
list.push(randomString(20, "abcdefghijklmnopqrstuvwxyz"));
}
$scope.list = list;
$scope.filteredList = list;
$scope.$watch('searchText', function(){pickFilter($scope);});
};
app.controller("MyCtrl", controller);