不能输出数组从http get url成功函数到html

问题描述:

我试图从成功函数输出结果如下,但我没有运气。该代码确实返回UpcomingEvents的值,如果我将它输出到html,它可以工作,但是当我将它传递到返回列表时,它不起作用。不能输出数组从http get url成功函数到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> 

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

谢谢。

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

然后,当您添加的项目您分配一个数组不是数组的一个项目:

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

因此,最终的returnlist看起来像:

{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