This question already has an answer here:
这个问题在这里已有答案:
- How to cancel an $http request in AngularJS? 8 answers
如何在AngularJS中取消$ http请求? 8个答案
So i have different states(with different controllers), each state fetching data from api. But if user leave state before request is resolved, then handler is called when he is not in this state. That's not intended. Moreover some of handlers have $intervals fetching data (after resolving 1st request) -> so the data keep fetching and fetching while user moved away hours ago. First thing that came to my mind:
所以我有不同的状态(使用不同的控制器),每个状态从api获取数据。但是如果用户在请求被解析之前离开状态,那么当他不处于这种状态时会调用处理程序。这不是故意的。此外,一些处理程序具有获取数据的$ interval(在解析第一个请求之后) - >因此数据在几小时前用户移开时继续获取和获取数据。我想到的第一件事:
let isDestroyed = false;
$http.get('/someurl').then(response => {
if (isDestroyed) return;
processResponse();
});
$scope.$on('$destroy',() => {
isDestroyed = true;
});
What else can i do?
我还可以做些什么?
1 个解决方案
#1
0
timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.
timeout - {number | Promise} - 超时(以毫秒为单位),或承诺在解决时应中止请求。
Meaning: set timeout to a promise, when destroying the scope - resolve the promise
含义:在销毁范围时将超时设置为承诺 - 解决承诺
#1
0
timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.
timeout - {number | Promise} - 超时(以毫秒为单位),或承诺在解决时应中止请求。
Meaning: set timeout to a promise, when destroying the scope - resolve the promise
含义:在销毁范围时将超时设置为承诺 - 解决承诺