[英]angularjs ng-repeat evaluated with ng-if
I'm looking for a way to do an ng-repeat with an expression. 我正在寻找一种使用表达式进行ng-repeat的方法。 Here is what I currently have. 这是我目前拥有的。
<tr ng-cloak id="{{$index}}"
ng-repeat-start="item in currentContoller.items |
orderBy: '-time'">
<td>item.a</td>
<td>item.b</td>
</tr>
what I am looking for is something like this, where firstItemOnly is a checkbox 我正在寻找的是这样的东西,其中firstItemOnly是一个复选框
<tr ng-cloak id="{{$index}}"
ng-repeat-start="item in currentContoller.items |
orderBy: '-time' |
if ({{firstItemOnly}}) {limitTo: 1}"> //this is the line that needs added
<td>item.a</td>
<td>item.b</td>
</tr>
The key to solving a problem like this is to think outside of the box. 解决此类问题的关键是跳出思维框。 Instead of trying to use an expression within the ng-repeat
, add an expression to the collection itself: 与其尝试在 ng-repeat
使用表达式, ng-repeat
在集合本身中添加一个表达式:
// create a function that returns the relevant collection of items
$scope.getItems = function() {
// if first item only, return the first item
if (this.firstItemOnly)
{
return this.items.slice(0, 1);
}
// otherwise, return all items
return this.items;
};
...
<tr ng-cloak id="{{$index}}"
ng-repeat-start="item in getItems() | orderBy: '-time'">
tried using $first ? 尝试使用$ first?
https://docs.angularjs.org/api/ng/directive/ngRepeat https://docs.angularjs.org/api/ng/directive/ngRepeat
use ng-if with $first and show a checkbox. 将ng-if与$ first一起使用,并显示一个复选框。 bob=auntie :) bob =阿姨:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.