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 –