简体   繁体   English

角度-多个$ http.get数据请求

[英]Angular - Multiple $http.get Requests for Data

What is the best approach to take when making multiple calls to an API for data needed in the same view? 对同一视图中需要的数据多次调用API时,最好的方法是什么?

For example, you have multiple select boxes which need to contain data pulled in from outside the app, all in the same view. 例如,您有多个选择框,这些选择框需要包含从应用程序外部提取的数据,并且都在同一视图中。

Is there a more elegant solution than simply firing them all at once in your controller? 是否有比简单地一次在控制器中将它们全部触发更好的解决方案? Such as the following 如以下

app.controller('myCtrl', function($service) { 

   $service.getDataOne().then(function(response) {
      $scope.dataOne = response;
   }, function(error) {
      console.log(error);
   });

   $service.getDataTwo().then(function(response) {
      $scope.dataTwo = response;
   }, function(error) {
      console.log(error);
   })
}); 

etc...with each service function performing a $http.get request. 等等...每个服务功能执行一个$ http.get请求。

While this works, I feel there is probably a more elegant solution. 虽然这可行,但我觉得可能还有一个更优雅的解决方案。

Any thoughts as always is much appreciated. 任何想法一如既往地受到赞赏。

You can use q.all() , as it accepts an array of promises that will only be resolved when all of the promises have been resolved. 您可以使用q.all() ,因为它接受一个仅在所有承诺均已解决后才能解决的承诺数组。

$q.all([
      $service.getDataOne(),
      $service.getDataTwo()
    ]).then(function(data){
      $scope.dataOne = data[0];
      $scope.dataTwo = data[1];
 });

If you look at the link, q.All() is at the very bottom of the page 如果您查看链接,则q.All()位于页面的底部

I believe you are looking for the $q service. 我相信您正在寻找$ q服务。

http://jsfiddle.net/Zenuka/pHEf9/21/ http://jsfiddle.net/Zenuka/pHEf9/21/

https://docs.angularjs.org/api/ng/service/ $q https://docs.angularjs.org/api/ng/service/ $ q

function TodoCtrl($scope, $q, $timeout) {
function createPromise(name, timeout, willSucceed) {
    $scope[name] = 'Running';
    var deferred = $q.defer();
    $timeout(function() {
        if (willSucceed) {
            $scope[name] = 'Completed';
            deferred.resolve(name);
        } else {
            $scope[name] = 'Failed';
            deferred.reject(name);
        }
    }, timeout * 1000);
    return deferred.promise;
}

// Create 5 promises
var promises = [];
var names = [];
for (var i = 1; i <= 5; i++) {
    var willSucceed = true;
    if (i == 2) willSucceed = false;
    promises.push(createPromise('Promise' + i, i, willSucceed));
}

// Wait for all promises    
$scope.Status1 = 'Waiting';
$scope.Status2 = 'Waiting';
$q.all(promises).then(
    function() { 
        $scope.Status1 = 'Done'; 
    }, 
    function() { 
        $scope.Status1 = 'Failed'; 
    }
).finally(function() {
    $scope.Status2 = 'Done waiting';
});
}

Credit: Code shamelessly stolen from unknown creator of fiddle. 信用:从未知的小提琴创作者无耻地偷来的代码。

If it for loading the looku pdata for all the dropdowns, I would make one call to get all the lookup data in one single payload. 如果它用于为所有下拉菜单加载looku pdata,我将进行一次调用以将所有查找数据存储在一个有效负载中。 Something like this. 这样的事情。 Each property value is an array of items for each dropdown. 每个属性值是每个下拉菜单项的数组。

{
    "eventMethods": [
        {
            "name": "In Person",
            "id": 1
        },
        {
            "name": "Phone",
            "id": 2
        }
    ],
    "statuses": [
        {
            "name": "Cancelled",
            "id": 42
        },
        {
            "name": "Complete",
            "id": 41
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM