在自定义指令中获取$ parent.$ index [英] Getting $parent.$index inside a custom directive
本文介绍了在自定义指令中获取$ parent.$ index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<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>
在上面的代码中,我无法在自定义指令中访问父级ng-repeat索引.如何获取父级ng-repeat的索引
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屋!
查看全文