无法从http get url成功函数输出数组到html上

时间:2017-09-10 01:00:38

标签: angularjs controller

我正在尝试输出成功函数的结果如下,但我没有运气。代码确实返回UpcomingEvents的值,如果我将其输出到html,它可以工作,但是当我将它传递到返回列表时,它不起作用。

    $scope.Events = {}
    $scope.returnlist = {};
    var PriorID = 67;
    var i = 0;
    var j = 0;

    //I am calling http get url function which is working fine.
       .success(function (response) {
        console.log(response);
        $scope.Events = response;
        //I am able to display Events[0].ID in html.

        if ($scope.Events[0].ID == PriorID)
        //The condition holds true, so it will go inside the loop. I have  
         confirmed that with a debugger.
        {
            newID = $scope.Events[0].ID;
            newname = $scope.Events[0].Title;
            $scope.returnlist[0] = [{ ID: newID, Name: newname }];
            $scope.returnlist[1] = [{ ID: newID, Name: newname }];
            //through debugger breakpoints, I have found that returnlist is  
            getting the correct values. 

        }

    })

在我尝试在html中显示值之前,一切正常。我这样试试。

                {{returnlist[0].ID}}

我甚至尝试过这样:

             <tr ng-repeat="data in returnlist">
              <td>returnlist.ID</td>

但没有运气。我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:1)

首先定义$scope.returnlist = {};这是一个对象而不是一个数组。

然后,当您添加项目时,您指定的数组不是数组项目:

$scope.returnlist[0] = [{ ID: newID, Name: newname }];

所以你的最终回复列表如下:

{0: [{ ID: newID, Name: newname }], 1:[{ ID: newID, Name: newname }],...}

一个具有数组值的对象,所以当你执行$ scope.returnlist [0] .ID时,你将得到未定义的。

正确的方法是:

$scope.returnlist = [];//an array

//in the loop now:
$scope.returnlist[0] = { ID: newID, Name: newname };//an item not an array