How to wait for a response in angularjs

What I am doing is placing the filter in a table (contains records). When I type in the search box (using the http get method) the table data is updated to match the search box. But when I type very quickly I see 500 Internal Server error in the console.

The main problem is that before posting the previous response, I run the next request. As a result, there are no problems. It's just that the console shows every HTTP request. And in case of fast typing in the search box, it gets red .

What's the solution for this?

+3


source to share


1 answer


you can turn off your search:



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>

      

+2


source







All Articles