angular.js - ngrepeat指定内容添加属性

浏览:26日期:2023-01-28

问题描述

<p ng-repeat='program in programs'><a href='https://www.6hehe.com/wenda/14765.html#'></a> </p>

programs里有10条数据,现在想在第5条和第10条的a标签上添加特定属性指令rightCtrl实现如下效果

<p><a href='https://www.6hehe.com/wenda/14765.html#'></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#'></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#'></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#'></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#' rightCtrl></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#'></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#'></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#'></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#'></a></p> <p><a href='https://www.6hehe.com/wenda/14765.html#' rightCtrl></a></p>

请问该如何实现

问题解答

回答1:

ng-repeat指令会遍历programs集合,然后给集合中的每一个元素都生成一个模板实例,集合programs中的每个元素都会被赋予自己的作用域和模板,同时每个每个模板实例的作用域都会暴露出一些属性。你可以利用其中的$index属性去完成你想要的事情。

我这里有个例子,你可以看看,在指令中判断要加入指令的的哪一个,然后写你的代码就好。demo

看代码可能要翻墙,如果浏览不了,我再给你截个图也行。

回答2:

<p ng-repeat='program in programs'> <a href='https://www.6hehe.com/wenda/14765.html#' ng-if=’$index == 4’ rightCtrl></a> <a href='https://www.6hehe.com/wenda/14765.html#' ng-if=’$index != 4’></a></p>回答3:

使用ng-if指令。

相关文章: