Skip to content

Instantly share code, notes, and snippets.

@yang-wei
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yang-wei/c4aa484b515435aaf659 to your computer and use it in GitHub Desktop.
Save yang-wei/c4aa484b515435aaf659 to your computer and use it in GitHub Desktop.
Using lodash debounce in ng-repeat
(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;
};
});
})();
@yang-wei
Copy link
Author

Using lodash debounce in ng repeat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment