小编J.S*_*.S.的帖子

AngularJS $http.get 异步执行顺序

我最近在 AngularJS 中做了很多编码。一段时间后,它开始对它感到满意,并且也变得非常高效。但不幸的是,有一件事我不明白:

在我的项目中,我需要通过 $http.get 和 RESTful API 服务器获取数据。这是我首先开始绊倒的地方。在处理需要继续的数据的函数中实现 promise($q.defer 等和 .then)后,我想我已经解决了这个问题。

但在这段代码中:

$scope.getObservationsByLocations = function() {
        var promise = $q.defer();
        var locationCount = 0;

        angular.forEach($scope.analysisData, function(loc) {   // for each location
            $http.get($scope.api + 'Device?_format=json', {     // get all devices
                params: {
                    location: loc.location.id
                }
            }).then(function (resultDevices) {
                var data = angular.fromJson(resultDevices);
                promise.resolve(data);
                // for each device in this location
                angular.forEach(angular.fromJson(resultDevices).data.entry.map(function (dev) {
                    http.get($scope.api + 'Observation?_format=json', {     // get all observations
                        params: {
                            device: dev.resource.id
                        }
                    }).then(function (resultObservations) {
                        var …
Run Code Online (Sandbox Code Playgroud)

javascript json angularjs hl7-fhir angularjs-http

6
推荐指数
1
解决办法
6485
查看次数

标签 统计

angularjs ×1

angularjs-http ×1

hl7-fhir ×1

javascript ×1

json ×1