ng-repeat

时间:2016-06-14 06:27:25

标签: angularjs

这是我的控制器

var i;

for (i = 1; i <= 3; i++ ) {
   $scope["items"+i].push({
      notification: item.notification,
      admin_id: item.admin_id,
      user_id: item.user_id,
      chat_time: item.chat_time
   })
}

这是我在ng-repeat中的静态范围

 <ul>
  <li ng-repeat="x in items1"></li>
  <li ng-repeat="x in items2"></li>
  <li ng-repeat="x in items3"></li>
 </ul>

如何在单个ng-repeat中使用ng-repeat制作动态迭代范围变量,如此

<ul>
  <li ng-repeat="x in items[i++]"> 
     <!-- generate from here-->
        <li ng-repeat="x in items1"></li>
        <li ng-repeat="x in items2"></li>
        <li ng-repeat="x in items3"></li> 
     <!-- to here -->
  </li>
</ul>

它将显示为我的静态范围

谢谢,感谢您的帮助和评论

1 个答案:

答案 0 :(得分:1)

您需要将所有不同的arrays存储在一个新数组中,然后,您可以如图所示循环它(我已经创建了一个示例)

<强> JS:

$scope.item1 = [ /* json array 1 */];

$scope.item2 = [ /* json array 2 */];

$scope.item3 = [ /* json array 3 */];

$scope.itemsList = [];
for (i = 0; i < 3; i++) {
  var varName = '$scope.item' + (i + 1);
  $scope.itemsList.push(eval(varName));
}

<强> HTML:

<li ng-repeat="mainList in itemsList">
      <p ng-repeat="specificList in mainList">
        <span>Id: {{specificList.id}}</span>
        <br />
        <span>Name: {{specificList.name}}</span>
      </p>
</li>

查看demo