[英]clear all pending $http requests and reload page in angularjs
我試過$ 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;
}
});
})();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.