AngularJS 两个 http 在一个控制器中获取问题 [英] AngularJS two http get in one controller make problems

查看:18
本文介绍了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 链.可以创建任意长度的链,并且由于一个承诺可以用另一个承诺来解决(这将进一步推迟其解决方案),因此可以在链中的任何点暂停/推迟承诺的解决.

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天全站免登陆