zoukankan      html  css  js  c++  java
  • 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 可以实现!

  • 相关阅读:
    Create方法失效而没有提示错误信息
    JS弹出窗口控制
    本周活动
    JavaScript的初步了解
    关于PHP接收文件的资料
    mvc模式改进网站结构
    一周动态
    排序
    Java的内存泄漏
    Android笔记
  • 原文地址:https://www.cnblogs.com/guofan/p/7692564.html
Copyright © 2011-2022 走看看