ng-repeat中的另一个Web服务调用

时间:2017-06-26 03:29:26

标签: javascript angularjs web-services

我有一个角度应用程序,我需要调用Web服务。我必须调用两个不同的url来获取数据。 我的第一个网址就像==> abc.com/student/3 这是学生名单。另一个URL是 abc.com/parent/idofStudent3 当我在第二个网址中传递3的学生ID时,我需要从第二个网址获取父级名字。

我可以检索第一个网址数据,但是我无法通过传递 ng-repeat 中的第一个记录数据来检索第二个网址数据。您能帮我解决一下如何在网页中检索父姓名吗?

Html Page

<h1 ng-repeat="x in myWelcome">
{{x.firstname}} || {{x.parentId}} </h1>

此处不是显示 parentid ,而是通过传递 parentId 作为参数来调用另一个Web服务来显示父名称。如何在这里打电话给另一个网络服务?

<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
        $http.get("abc.com/student/3")
            .then(function(response) {
                $scope.myWelcome = response.data;
            });
    });
</script>

==&GT;第一个Web服务响应如下:

   {
    "student":[
    {
         "name":"john",
          "parentId": 12,
    "address":"NYC"
},

    {
         "name":"Rohi",
          "parentId": 14,
    "address":"NJ"
},
 ]
 }

==&GT;当parentID = 12时,第二个webservice响应是这样的:

{
"firstName": "Sr. John",

}
======> when parentIS 14
{
"firstName": "Sr. Rohi",

}


-------------------------
firstname || parentName
-------------------------
John      ||  Sr. John
Rohi      ||   Sr. Rohi

2 个答案:

答案 0 :(得分:0)

目前,您正在错误的范围对象中搜索Parent details; myWelcome对象包含来自第一次服务调用的数据,该数据仅返回list of student个详细信息。

您可以定义一个新的范围对象,并在第二个服务调用中将数据绑定到该对象,该服务调用将包含父详细信息然后,您可以在ng-repeat中使用该对象。

答案 1 :(得分:0)

<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
         $scope.myWelcome = []
        $http.get("abc.com/student/3")
            .then(function(response) {
                $scope.StudentData = response.data;
          if($scope.StudentData){
                  $scope.myWelcome.push($scope.StudentData)
                  $http.get("abc.com/parent/$scope.StudentData.studentId")
                     .then(function(response) {
                       $scope.parentData = response.data;
                        $scope.myWelcome.push($scope.parentData)

            });
          }
        });
    });
</script>

我想这可能对你有帮助