angularjs显示嵌套数组的索引号

时间:2017-07-21 11:04:11

标签: javascript angularjs arrays nested

我有一个嵌套数组,如下所示: ***编辑阵列*

nested = {
  "_id": 24,
  "itemId": 15,
  "testGroups": [
    {
      "TestGroupName": null,
      "testGroup": [
        {
          "TestName": "ddasdasd",
          "TestType": 1,
                "testGroup": []
        },
        {
          "TestName": "dsadasddasd",
          "TestType": 2,
                   "testGroup": []
        },
        {
          "TestName": "adsdasd",
          "TestType": 0,
                    "testGroup": [
            {
              "TestGroupName": "dasdasd",
              "testGroup": [
                {
                  "TestName": "dasdasd",
                  "TestType": 1,
                                    "testGroup": []
                }
              ]
            },
            {
              "TestGroupName": "dasdasd",
              "testGroup": [
                {
                  "TestName": "dasdasd",
                  "TestType": 2,
               "testGroup": []
                }
              ]
            }
          ]
        },
        {
          "TestName": "dasdasddasd",
          "TestType": 2,
          "testGroup": []
        }
      ]
    }
  ]
}

我需要为TestName

显示索引和子索引为 1.1,1.2,1.2.1 ......

我尝试使用$parent.$index$index的组合,但它不适用于子项目,因为它将0显示为索引。

1 个答案:

答案 0 :(得分:0)

使用ng-init

为变量分配索引
 <div ng-repeat="item in arr" ng-init="parentIndex = $index" >
    <div ng-repeat="subitem in item.subitems" ng-init="chilIndex = $index">
    {{parentIndex}}.{{chilIndex}}
     </div>
 </div>

演示

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.arr = [  
   {  
      item:"",
      subitems:[  
         {  
            item:"",
            subitems:[  
               {  
                  item:"",
                  subitems:[  
                     {  
                        item:"",
                        subitems:[  

                        ]
                     },
                     {  
                        item:"",
                        subitems:[  

                        ]
                     }

                  ]
               }
            ]
         },
         {  
            item:"",
            subitems:[  

            ]
         }

      ]
   },
   {  
      item:"",
      subitems:[  
         {  
            item:"",
            subitems:[  

            ]
         },
         {  
            item:"",
            subitems:[  

            ]
         }
      ]
   }
]
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <div ng-repeat="item in arr" ng-init="parentIndex = $index" >
    <div ng-repeat="subitem in item.subitems" ng-init="chilIndex = $index">
    {{parentIndex}}.{{chilIndex}}
     </div>
 </div>
</div>