3

What I am doing is putting the filter on table (contains records). When I type into search box (used http get method), table data get updated as per search box. But when I type very fast, then I see in console 500 Internal Server error.

The main problem is before comming previous response I am firing the next request. There is no problem in result. Just console shows every http request. And in case of fast typing in search box it gets red.

What is the solution for it?

4
  • 2
    you can keep a scope variable which will set to true when get call is in process and after running then method that variable will again become false i.e we can again make a call. Commented Jul 7, 2015 at 12:24
  • You can use something called as chained promise using $q Commented Jul 7, 2015 at 12:33
  • 1
    not sure if that's gonna work but when you apply angular filter you can use debounce option like that: <input type="text" ng-model="searchStr" ng-model-options="{debounce: 1000}"> which will only filter after 1000ms after you stopped typing Commented Jul 7, 2015 at 12:35
  • set boolean loading=true before searching, if boolean if true, do not search again. on getting results back set false... Commented Jul 7, 2015 at 12:56

1 Answer 1

2

you could trottle your search input :

var app = angular.module('app', []);

    app.controller('TableController', function($scope, $http, $timeout) {
        $http.get('yourTableData.json').then(function(result){
            $scope.rows = result.data;
        });

        $scope.filterText = '';

        var temp = '',
            filterTextTimeout;
        $scope.$watch('searchText', function (val) {
            if (filterTextTimeout){
               $timeout.cancel(filterTextTimeout);
            }    
            temp = val;
            filterTextTimeout = $timeout(function() {
                $scope.filterText = temp;
            $http.get($scope.filterText).then(function(result){ 
            $scope.rows = result;
            }
            }, 250);
        })
    });

    <input id="searchInput" type="search" placeholder="search field" ng-model="searchText" />
    <div class="record" ng-repeat="row in rows | filter:filterText">
        <span>{{row.content}}</span>
    </div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.