http调用中的Angular http调用

时间:2017-06-20 16:21:51

标签: angularjs angular-promise angular-services

我在我的服务中设置了一个函数,用于返回与某个应用程序相关的服务器/主机列表。出于前端目的,我一直在尝试为主机分配颜色,具体取决于该主机上正在运行的服务数量/警告/严重。为了实现这一点,我首先进行api调用以获取与该应用程序相关的所有主机,然后循环返回的主机列表并执行另一个api调用以获取服务。

我的问题是他们正在以正确的顺序解析,所以我的Data2变量正在返回" undefined"。如何在第一个for循环中解决它,以便为每个主机分配状态颜色?

有没有更好的方法来实现这个?

这是我在服务中定义的功能。

// Function to get Servers and all their information **********************************
            service.getHosts = function(AppName){
                var HostList = [];

//intial http call to get the correct hostlist associated with the selected application ************
                var promise = $http.get('http://localhost:9000/App/' + AppName);
                promise.then(function(response){
                        var Data = response.data.recordset;

//Looping through each host is the recordset to push them into the HostList[] **********************
                        for (i = 0; i <= Data.length -1; i++){
                            //variables for the loop    
                            var StatusColor = '';
                            var StatusTextColor = '';
                            var count = 0;

//another http call to get the services for each host in the Hostlist ******************************
                            $http.get('http://localhost:9000/Service/' + Data[i].HostName)
                            .then(function(response){
                                var Data2 = response.recordset;
//looping through the services to see if any of the services have status other than ok (shortstatus != 0) ********
                                for(i = 0; i<= Data2.length-1; i++){
                                    if(Data2[i].ShortStatus != 0){
                                        count = count + 1;
                                    }
                                }

//Assigning the status color for each host depending on how many services are not ok (either warning or critical) *************
                                if (count == 0){
                                    StatusColor ='rgb(255,152,0)';
                                    StatusTextColor = 'black';
                                }else if (count == 1){
                                    StatusColor ='rgb(255,152,0)';
                                    StatusTextColor = 'white';
                                }else{
                                    StatusColor = 'rgb(244,67,54)';
                                    StatusTextColor = 'white';
                                }
//Pushing host information and status color to the HostList **********************            
                                HostList.push({
                                "address":Data[i].Address,
                                "hostname":Data[i].HostName.split('.')[0],
                                "fullhostname":Data[i].HostName,
                                "statuscolor":StatusColor,
    //                            "textcolor":'black'
                                })    
                            });


                        }
                    })
                return HostList;
            };

非常感谢任何帮助,或者任何更简单或更优雅方式的建议都会很棒。

2 个答案:

答案 0 :(得分:2)

使用$ q.all并保证链接

{result, "failCount":.actions[2].failCount, "skipCount":.actions[2].skipCount, "totalCount": .actions[2].totalCount}

答案 1 :(得分:0)

可以使用return statement

来链接Promise
$http.get(url).then(function(response) {
    return $http.get(url2);
}).then(function(response2) {
    return $http.get(url3);
}).then(function(response3) {
    //...
});

有关详细信息,请参阅