Last active
August 29, 2015 14:06
-
-
Save yang-wei/c4aa484b515435aaf659 to your computer and use it in GitHub Desktop.
Using lodash debounce in ng-repeat
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(){ | |
/* Todo: fix performance */ | |
angular.module('app', []) | |
.controller('mainCtrl', function($scope){ | |
$scope.countries = [ | |
{ country: 'Japan', language: 'Japanese' }, | |
{ country: 'Korea', language: 'Korean' }, | |
{ country: 'North Korea', language: 'Korean'}, | |
{ country: 'China', language: 'Chinese'}, | |
{ country: 'Hong Kong', language: 'Cantonese'}, | |
{ country: 'Sinagpore', language: 'English'}, | |
{ country: 'Thailand', language: 'Thai'}, | |
{ country: 'Malaysia', language: 'Malay'}, | |
{ country: 'Brunei', language: 'Malay'} | |
]; | |
/* show loading indicator */ | |
$scope.loading = false; | |
$scope.$watch('searchQuery', function(x,y){ | |
if(x !== y) $scope.loading = true; | |
}); | |
$scope.$watch('searchQuery', _.debounce(function(newV, oldV){ | |
if(newV == oldV) return; | |
$scope.$apply(function(){ | |
updateQuery(); | |
$scope.loading = false; | |
}); | |
}, 1000)); | |
var updateQuery = function() { | |
$scope.search = $scope.searchQuery; | |
}; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using lodash debounce in ng repeat