[英]How to access index in a component called in ng-repeat in angularjs?
I have the following files. 我有以下文件。 This is code in node-list.html 这是node-list.html中的代码
<tbody sv-node-list-item ng-repeat="item in items" />
And this is code in node-list-item.html. 这是node-list-item.html中的代码。
<tr><td>{{$index}}</td></tr>
Basically node-list-item is rendered for each iteration of the ng-repeat of the node-list.html file. 基本上,为node-list.html文件的ng-repeat的每次迭代呈现node-list-item。 I need to access the index of the item from the parent ie node-list.html in node-list-item.html I have been researching this for quiet some time but am not able to do so. 我需要从父节点(即node-list-item.html中的node-list.html)访问项目的索引。我已经进行了一段时间的研究,但无法这样做。 Please help. 请帮忙。 I am using angular1 ie angularjs. 我正在使用angular1即angularjs。 This is the directive code in node-list-item.js 这是node-list-item.js中的指令代码
function directive() { return { templateUrl: "partials/node-list-item.html" };}
module.directive("svNodeListItem",[ directive ]);
Use this in the node-list.html
:(You need to send index to the node-list-item directive) 在node-list.html
使用它:(您需要将索引发送到node-list-item指令)
<tbody ng-repeat="item in items track by $index" sv-node-list-item index="{{$index}}"/>
Then change your sv-node-list-item
directive to this: 然后将sv-node-list-item
指令更改为此:
function directive() {
return {
scope: {
index: '@'
},
templateUrl: "partials/node-list-item.html"
};
}
module.directive("svNodeListItem",[ directive ]);
And finally your node-list-item.html
to this: 最后是您的node-list-item.html
:
<tr><td>{{index}}</td></tr>
For more information about ng-repeat check the documentation here and for directive variables here . 有关更多信息,NG-重复检查的文件在这里和指令变量这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.