AngularJS:连续发送$ http.get请求直到某个条件 [英] AngularJS: Send $http.get requests continuously until certain condition

查看:164
本文介绍了AngularJS:连续发送$ http.get请求直到某个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想发送$ http.get请求到服务器,直到返回的结果符合条件。

As the title says, I want to send $http.get requests to the server until the returned results meets a condition.

这就是我试过的:

    var searchComplete = false;
    var sendGetReq = true;

    while(!searchComplete) // #2 and then jumps here
    {
       if(sendGetReq)
       {
         sendGetReq = false;  // #1 execution reaches here
         $http.get('/user/getCondition', {params: {condition: $scope.condition}).
        success(function (condition, status, headers, config) {
         ...
         ...
         if(condition)
         { searchComplete = true; }
         else
         { sendGetReq = true; }
    }
}



但是,$ http.get请求永远不会执行,这个语句永远不会到达,当我调试时,我看到执行将会到 sendGetReq = false ,然后跳转到 while(!searchComplete)。GET请求永远不会被发送。

However, the $http.get request never goes. That statement is never reached. When I debugged, I saw that execution will come to sendGetReq=false and then jump to while(!searchComplete). The GET request is never sent.

有人请解释:


  1. 为什么执行永远不会到达GET请求并跳回

  2. 建议连续执行此操作的方式是什么GET请求

谢谢。

Thank you.

推荐答案

为什么不把它们放在一个函数中,并在条件满足的情况下自行调用呢?就像这样,你应该使用.then而不是.success:

Why not put it all together inside a function and call itself if condition is meet? Like this and you should use .then and not .success:

$scope.load = function(){
  $http.get('/user/getCondition', {params: {condition: $scope.condition}).
    then(function (condition, status, headers, config) {

    if (condition){
       $scope.load();
    }

  });
}

希望它有帮助=)

这篇关于AngularJS:连续发送$ http.get请求直到某个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆