2014-10-01 64 views
0

我试图推动一些JSON数据到范围列表whiteout(后点击加载下一个项目btw)以追加列表和列表(使用Ionic框架和infiniteScroll)Angular如何将JSON数组值存入现有的范围?

有人可以告诉我,我做错了什么以及如何追加新的列表项到最后?

感谢您的任何建议。

JSON数组例如:

var fakeListData = [ 
     { "DATE" : testDateTimeFormated, 
      "NUMBER_OF_ALL_CALLS" : 25, 
      "RESULT_DONE" : 0, 
      "RESULT_NOT_INTERESTED" : 0, 
      "RESULT_NO_APP" : 0 
     }, 
     { "DATE" : testDateTimeFormated, 
      "NUMBER_OF_ALL_CALLS" : 0, 
      "RESULT_DONE" : 0, 
      "RESULT_NOT_INTERESTED" : 0, 
      "RESULT_NO_APP" : 0 
     }]; 

项目填写:

// Option one (throwing Chrome sendrequest error: TypeError: Converting circular structure to JSON) 
    $scope.listData.push(fakeListData); 

// Option two (crash browser)  
angular.forEach(fakeListData,function(item) { 
    $scope.listData.push(item); 
}); 
+0

做你宣布$ scope.listData – worldask 2014-10-01 12:10:23

+2

检查是否有您的JSON值是指相同的JSON。通常情况下会显示此错误。 – Gilsha 2014-10-01 12:17:42

+0

我创建了具有相同值的新JSON变量,并且出现以下错误: 错误:[ngRepeat:dupes]不允许在中继器中进行复制。使用'track by'表达式来指定唯一键。 Repeater:listData中的listDataItem,Duplicate key:object:01C,重复值: – 2014-10-01 12:33:36

回答

1

试试这个

$scope.listData=[]; 

fakeListData.forEach(function(item){ 
    $scope.listData.push(item); 
}) 
0

我们可以把更多的项目到现有的阵列,但语法应该像t他:

// instead of this 
// $scope.listData.push(fakeListData); 

// we should use this 
[].push.apply($scope.listData, fakeListData); 

注意:另外,重要的是要提及Angular JS。

如果我们要清除的阵列,同时保持对它的引用,我们应该去做这样的

// instead of this, which creates new array/new reference 
// $scope.listData = []; 

// we should use this, which will keep the reference, but clear its content 
$scope.listData.length = 0 

这样,$范围一生中,我们可以重新填写阵列和所有角手表会refelect得当

检查这个问题,以及:Short way to replace content of an array