1
我試過$ route.reload(); location.reload(真); $ window.location.reload(true);但掛起的請求不取消/中止,頁面正在重新加載,如何重新加載頁面意味着關閉所有正在掛起的$ http請求並重新加載頁面。
我試過$ route.reload(); location.reload(真); $ window.location.reload(true);但掛起的請求不取消/中止,頁面正在重新加載,如何重新加載頁面意味着關閉所有正在掛起的$ http請求並重新加載頁面。
這應該對你有用。當你想取消你的請求時,你需要調用abortAllPendingRequests
方法。
請記住,此解決方案確實需要使用下劃線。我沒有嘗試過與lodash。
(function()
{
angular.module('myApp')
.run(function ($http, $q)
{
$http.currentlyPendingRequests = [];
$http.accessPendingRequests = function (pending)
{
if (!arguments.length) return this.currentlyPendingRequests;
this.currentlyPendingRequests = pending;
};
$http.removeRequest = function (requestUrl)
{
this.currentlyPendingRequests = _.filter(this.currentlyPendingRequests, function (request)
{
return request.url !== requestUrl;
}, this)
};
$http.abortAllPendingRequests = function()
{
_.each(this.currentlyPendingRequests, function (request)
{
request.aborter.resolve();
});
this.currentlyPendingRequests = [];
};
var originalGet = $http.get;
$http.get = function()
{
// Ignore template requests
if (arguments[0].indexOf('.html') != -1)
{
return originalGet.apply(this, arguments)
};
var aborter = $q.defer();
var url = arguments[0];
this.currentlyPendingRequests.push({url : url, aborter : aborter});
console.log('pushing url : ' + url);
// Include the abortion promise in the new arguments
var newArgs = [arguments[0], _.extend({}, arguments[1], {timeout : aborter.promise})];
var requestPromise = originalGet.apply(this, newArgs);
// Finally is a reserved word and is not es3 compatible, and therefore non compliant for ie8. Thus the hash
// syntax must be used.
requestPromise['finally'](function()
{
this.removeRequest(url);
});
return requestPromise;
}
});
})();
不錯,它適用於我,謝謝! – Bradley
看一看abort()方法:http://stackoverflow.com/a/446626/2674883 –
是啊,像我放棄一個需要全局函數爲$ HTTP,這樣我可以在任何地方打電話? –
http://stackoverflow.com/questions/13928057/how-do-to-cancel-an-http-request-in-angularjs –