AngularJS两个http进入一个控制器时出现问题 [英] AngularJS two http get in one controller make problems

查看:151
本文介绍了AngularJS两个http进入一个控制器时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个控制器中有两个http GET,有时它可以工作,其中两个正在工作.有时只有一个http Get可以工作.有时没有显示出来. 有什么建议吗?

i have two http GET in one controller and sometimes it works and two of them are working. sometime only one http Get is work. and sometimes none of them is shown. any suggestions?

 }).controller("nextSidorAdminCtrl", 
 function($scope,$rootScope,$http,$location,$state) {
   $http.get("/ShiftWeb/rest/admin/getallsettingtime")
  .then(function(response) {
    $scope.settingtimes = response.data;
 });    
 $http.get("/ShiftWeb/rest/admin/nextsidor")
    .then(function(response) {
    $scope.nextsidor = response.data;
 });

图片:

https://prnt.sc/k5ewd6

推荐答案

绑定两个$ http.get操作:

Chain the two $http.get operations:

}).controller("nextSidorAdminCtrl", 
   function($scope,$rootScope,$http,$location,$state) {
       $http.get("/ShiftWeb/rest/admin/getallsettingtime")
       .then(function(response) {
           $scope.settingtimes = response.data;
           return $http.get("/ShiftWeb/rest/admin/nextsidor")
       })    
       .then(function(response) {
           $scope.nextsidor = response.data;
       });

由于调用promise的.then方法将返回新的派生promise,因此很容易创建promise的promise.可以创建任意长度的链,并且由于一个承诺可以用另一个承诺来解决(这将进一步推迟其解决方案),因此可以在链中的任何点处暂停/推迟对这些承诺的解决.

Because calling the .then method of a promise returns a new derived promise, it is easily possible to create a chain of promises. It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain.

有关更多信息,请参见 AngularJS $ q服务API参考-链接承诺

For more information, see AngularJS $q Service API Reference - chaining promises

这篇关于AngularJS两个http进入一个控制器时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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