首页 > 其他 > 详细

ng-repeat嵌套的$index

时间:2017-10-19 15:50:00      阅读:285      评论:0      收藏:0      [点我收藏+]

  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

原文:http://www.cnblogs.com/guofan/p/7692564.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!