Jak można anulować bieżące żądanie kątowe $ http, gdy pojawi się nowe żądanie?
Mam aplikację Angular z widokiem, że aktualizacje są wyświetlane w trybie użytkownika. Czasami stare żądania są uzupełniane po ostatnim żądaniu, co oznacza, że widok wyświetla nieprawidłowe dane. Jaki jest najprostszy sposób na anulowanie poprzedniego żądania, gdy pojawi się nowy?
Korzystanie z Angular 1.5, o ile warto.
<input ng-model = "query" ng-keyup = "search()"/>
{{results | json}}
// In the controller:
$scope.search = function(){
$http({
method: "GET",
url: "/endpoint.php"
params: {
query: $scope.query
}
}).then(function(response){
$scope.results = response.data;
})
}
Jedno rozwiązanie próbowałem:
// In the controller:
var canceler = $q.resolve(); // New
$scope.search = function(){
canceler.resolve("Canceling old request"); // New
$http({
method: "GET",
url: "/endpoint.php"
params: {
query: $scope.query
},
timeout: canceler.promise // New
}).then(function(response){
$scope.results = response.data;
})
}
W tym scenariuszu, chociaż ja dzwonię canceler.resolve zanim wniosek $ http, wniosek pojawia się jako "failed".
Jakieś spostrzeżenia?
edytuj: Rozwiązanie zostało znalezione!
// In the controller:
var canceler = $q.defer();
$scope.search = function(){
canceler.resolve("cancelled"); // Resolve the previous canceler
canceler = $q.defer(); // Important: Create a new canceler!
// Otherwise all $http requests made will fail
$http({
method: "GET",
url: "/endpoint.php"
params: {
query: $scope.query
}
}).then(function(response){
$scope.results = response.data;
})
}
Hmm - Staram this- 'ng-click = "cancel(); search();"' a ja wciąż to samo XHR zawiodły w mojej konsoli . Wstrzyknęłam też $ q. – schnauss
@schnauss używasz anulatora var = $ q.resolve() lub var canceler = $ q.defer() ;? – NMSL
'var canceler = $ q.defer();'. Jestem zdumiony. – schnauss