在自定义指令中获取 $parent.$index [英] Getting $parent.$index inside a custom directive

查看:18
本文介绍了在自定义指令中获取 $parent.$index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • <div ng-repeat="j in array2"><div 示例指令 ><p>{{$index}} ,{{$parent.$index}}</p>
  • 在上面的代码中,我无法访问自定义指令中的父 ng-repeat 索引.如何获取父 ng-repeat 的索引

    解决方案

    此示例可以帮助您弄清楚如何在指令中获取索引等.

    var app = angular.module("app", []);app.controller("controller", function ($scope) {$scope.array1 = [{id: "1-1"},{id:1-2"}];$scope.array2 = [{id: "2-1"},{id:2-2"}];});app.directive("exampleDirective", function () {返回 {限制:A",范围: {示例指令:="},链接:函数(范围、元素、属性、ngModel){控制台.日志(范围.exampleDirective)}}})

    <html ng-app="app" ng-controller="controller as ctrl"><头><title></title><身体><ul><li ng-repeat="array1 track by $index 中的值">{{value.id}}<ul><li ng-repeat="j in array2"><div example-directive="{parentIndex: $parent.$index, childIndex: $index}">{{j.id}}<p>array1 索引:{{$parent.$index}}</p><p>array2 索引:{{$index}}</p>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script></html>

    <li ng-repeat="value in array1 track by $index">
    <div ng-repeat="j in array2">
            <div example-directive >
                    <p>     {{$index}} ,{{$parent.$index}}</p>
            </div>
    </div>
    </li>
    

    In the above code I couldnt access parent ng-repeat index inside my custom directive.how can I get the index of parent ng-repeat

    解决方案

    This sample can helps you to figure out how can get index or etc ... in directives.

    var app = angular.module("app", []);
    
            app.controller("controller", function ($scope) {
    
                $scope.array1 = [
                    {id: "1-1"},
                    {id: "1-2"}
                ];
    
                $scope.array2 = [
                    {id: "2-1"},
                    {id: "2-2"}
                ];
    
            });
    
            app.directive("exampleDirective", function () {
                return {
                    restrict: "A",
                    scope: {
                        exampleDirective: "="
                    },
                    link: function (scope, element, attr, ngModel) {
                        console.log(scope.exampleDirective)
                    }
                }
            })

    <!DOCTYPE html>
    <html ng-app="app" ng-controller="controller as ctrl">
    <head>
        <title></title>
    </head>
    <body>
    
        <ul>
            <li ng-repeat="value in array1 track by $index">
                {{value.id}}
                <ul>
                    <li ng-repeat="j in array2">
                        <div example-directive="{parentIndex: $parent.$index, childIndex: $index}">
                            {{j.id}}
                            <p>array1 index: {{$parent.$index}}</p>
                            <p>array2 index: {{$index}}</p>
                        </div>
                    </li>
                </ul>
            </li>
        </ul>
    
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    </body>
    </html>

    这篇关于在自定义指令中获取 $parent.$index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    其他开发最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆