新しいリクエストがあったときに、継続的な$ httpリクエストをキャンセルするにはどうすればよいですか?
私は更新がユーザーの種類として、ライブビューでの角度のアプリを持っています。最新のリクエストの後に古いリクエストが完了することがあります。つまり、ビューに間違ったデータが表示されることがあります。新しいリクエストがあるときに前のリクエストをキャンセルする最も簡単な方法は何ですか?
角度1.5を使用すると、それは価値があります。私が試してみました
<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;
})
}
一つの解決策:このシナリオでは
// 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;
})
}
、私は$のhttpリクエストの前にcanceler.resolveを呼んでいるにもかかわらずは、要求が「失敗」としてアップになります。
任意の洞察力?
編集:解決策が見つかりました!
// 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;
})
}
うーんを - 私はthis- 'NGクリック=試みる'と私はまだ同じXHRは私のコンソールに失敗します "キャンセル();検索()" 。私は$ qも注射しています。 – schnauss
@schnaussは、var canceler = $ q.resolve()またはvar canceler = $ q.defer();を使用していますか? – NMSL
'var canceler = $ q.defer();'。私は困惑している。 – schnauss