ng-repeat嵌套的$index


  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 可以實現!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM