1. 程式人生 > >ng-repeat嵌套的$index

ng-repeat嵌套的$index

tro 維數 clas 下標 div 我們 logs -1 數組下標

  angular應用中,ng-repeat很方便的實現了數組的展示:

<div ng-controller=‘myCtrl‘>
    <ul>
        <li ng-repeat="obj in list">
        {{obj.name}}
        <ul>
            <li ng-repeat="child in obj.children" ng-click="childClick($index, child, $parent.$index)">
            {{child.name}}
                
</li> </ul> </li> </ul> </div> <script> angular.module("myApp",[]) .controller("myCtrl",function($scope){ $scope.list = [ {name: 1, children: [{name:1-1}]}, {name: 2, children: [{name:2-2}]} ] $scope.childClick
= function (index, obj, parentIndex) { console.log(index,obj,parentIndex); } }) </script>

  這裏我們用到了ng-repeat嵌套來實現二維數組,而定位顯然使用二維數組下標是最好不過的,裏層的ng-repeat使用 $index 記錄下標位置沒錯,那麽如果將外層的 $index 也傳入記錄呢? $parent.$index 可以實現!

ng-repeat嵌套的$index