在自定义指令中获取 $parent.$index [英] Getting $parent.$index inside a custom directive
本文介绍了在自定义指令中获取 $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>