AngularJs服务器请求队列或函数未调用(AngularJs server request queue or function is not call)

编程入门 行业动态 更新时间:2024-10-23 17:23:43
AngularJs服务器请求队列或函数未调用(AngularJs server request queue or function is not call)

我在套接字事件中订阅了我的控制器。 当某个事件发生时,我需要从服务器获取一些数据(我尝试将lb.get()作为函数调用到某个工厂)。

$scope.counter = 0;

$scope.$on('lEvent', function (event, response) { // socket event
                $scope.counter ++;
                console.log('counter '+$scope.counter);

                lb.get(response[0]).then(function(response){
                    var Item = {
                        id: response.id,
                        mime: response.mime,
                        name: response.name,
                    };
                    $scope.items.push(Item);
                    console.log("$scope.items"+$scope.items.length);
                });
    });


// here is a function in my factory 

 get: function(id) {
            deferred = $q.defer();
            $http({
                method: "post",
                url: url,
                data:  $.param({id: id}),
                headers: header
            })
                .success(function (data) {
                    deferred.resolve(data);
                })
                .error(function (data) {
                    deferred.reject(data);
                });
            return deferred.promise;
        } 
  
 

想象一下,我有5个套接字事件,但是函数lb.get()调用了4(或3)次而不是5次。您可以在控制台中看到调用的结果:

如您所见,函数lb.get()被调用了4次而不是5次。

我想,我需要像请求队列这样的东西。

I've subscribed in my controller on socket event. When some event has come, i need to get some data from server (i try to call lb.get() as a function into some factory).

$scope.counter = 0;

$scope.$on('lEvent', function (event, response) { // socket event
                $scope.counter ++;
                console.log('counter '+$scope.counter);

                lb.get(response[0]).then(function(response){
                    var Item = {
                        id: response.id,
                        mime: response.mime,
                        name: response.name,
                    };
                    $scope.items.push(Item);
                    console.log("$scope.items"+$scope.items.length);
                });
    });


// here is a function in my factory 

 get: function(id) {
            deferred = $q.defer();
            $http({
                method: "post",
                url: url,
                data:  $.param({id: id}),
                headers: header
            })
                .success(function (data) {
                    deferred.resolve(data);
                })
                .error(function (data) {
                    deferred.reject(data);
                });
            return deferred.promise;
        } 
  
 

Imagine, i've got 5 socket events, but function lb.get() has called a 4 (or 3) times instead of 5. You can see the result of calling in console:

As you can see, the function lb.get() was called 4 times instead of 5.

I think, i need something like a request queue.

最满意答案

您没有错误响应方法get handle 。 也许在这种情况下,你的回答就会消失。

您不需要请求队列。

请参阅jsfiddle上的示例 。

angular.module('ExampleApp', [])
  .controller('ExampleOneController', function($scope, ServiceExample) {
    $scope.counter = 0;
    $scope.successCounter = 0;
    $scope.errorCounter = 0;
    $scope.$on('raise.event', function(event, value) {
      console.log('counter', $scope.counter);
      $scope.counter++;
      ServiceExample.get(value).then(function() {
        console.log('success response:', $scope.successCounter);
        $scope.successCounter++;
      }).catch(function() {
        console.log('error response:', $scope.errorCounter);
        $scope.errorCounter++;
      });
    });
  })
  .controller('ExampleTwoController', function($scope) {
  $scope.raiseValue = "www.google.com"
    $scope.raise = function(val) {
      $scope.$emit('raise.event', val);
    };
  })
  .service('ServiceExample', function($http) {
    return {
      get: function(url) {
        return $http({
          method: "GET",
          url: url
        });
      }
    }
  }); 
  
.errors {
  color: maroon
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="ExampleApp">
  <div ng-controller="ExampleOneController">
    <h3>
      ExampleOneController
    </h3>
    <form name="ExampleForm" id="ExampleForm">
    <pre>counter : {{counter}}</pre>
    <pre>successCounter: {{successCounter}}</pre>
    <pre class="errors">errorCounter: {{errorCounter}}</pre>
    </form>

    <div ng-controller="ExampleTwoController">
      <h3>
      ExampleTwoController
    </h3>
      <form name="ExampleForm" id="ExampleForm">
        <input ng-model="raiseValue">
        <br>
        <button ng-click="raise(raiseValue)" simple>
          Send request to "{{raiseValue}}"
        </button>
      </form>
    </div>
  </div>
</body> 
  
 

You don't have handle for the error response method get. Maybe in this case, your response is disappear.

You don't need a request queue.

See example on jsfiddle.

angular.module('ExampleApp', [])
  .controller('ExampleOneController', function($scope, ServiceExample) {
    $scope.counter = 0;
    $scope.successCounter = 0;
    $scope.errorCounter = 0;
    $scope.$on('raise.event', function(event, value) {
      console.log('counter', $scope.counter);
      $scope.counter++;
      ServiceExample.get(value).then(function() {
        console.log('success response:', $scope.successCounter);
        $scope.successCounter++;
      }).catch(function() {
        console.log('error response:', $scope.errorCounter);
        $scope.errorCounter++;
      });
    });
  })
  .controller('ExampleTwoController', function($scope) {
  $scope.raiseValue = "www.google.com"
    $scope.raise = function(val) {
      $scope.$emit('raise.event', val);
    };
  })
  .service('ServiceExample', function($http) {
    return {
      get: function(url) {
        return $http({
          method: "GET",
          url: url
        });
      }
    }
  }); 
  
.errors {
  color: maroon
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="ExampleApp">
  <div ng-controller="ExampleOneController">
    <h3>
      ExampleOneController
    </h3>
    <form name="ExampleForm" id="ExampleForm">
    <pre>counter : {{counter}}</pre>
    <pre>successCounter: {{successCounter}}</pre>
    <pre class="errors">errorCounter: {{errorCounter}}</pre>
    </form>

    <div ng-controller="ExampleTwoController">
      <h3>
      ExampleTwoController
    </h3>
      <form name="ExampleForm" id="ExampleForm">
        <input ng-model="raiseValue">
        <br>
        <button ng-click="raise(raiseValue)" simple>
          Send request to "{{raiseValue}}"
        </button>
      </form>
    </div>
  </div>
</body> 
  
 

更多推荐

本文发布于:2023-07-25 06:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1257265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   函数   服务器   AngularJs   server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!