Javascript angularjs ng重复使用ng if进行计算
我正在寻找一种用表达式重复ng的方法。这是我目前拥有的Javascript angularjs ng重复使用ng if进行计算,javascript,angularjs,Javascript,Angularjs,我正在寻找一种用表达式重复ng的方法。这是我目前拥有的 <tr ng-cloak id="{{$index}}" ng-repeat-start="item in currentContoller.items | orderBy: '-time'"> <td>item.a</td> <td>item.b</td> </tr> 项目a 项目b 我要找的是这样的东西
<tr ng-cloak id="{{$index}}"
ng-repeat-start="item in currentContoller.items |
orderBy: '-time'">
<td>item.a</td>
<td>item.b</td>
</tr>
项目a
项目b
我要找的是这样的东西,其中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>
//这是需要添加的行
项目a
项目b
解决这样的问题的关键是跳出框框思考。不要尝试在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'">
//创建一个返回相关项集合的函数
$scope.getItems=函数(){
//如果仅第一项,则返回第一项
如果(仅此.firstItem)
{
返回此.items.slice(0,1);
}
//否则,返回所有项目
归还此物品;
};
...
是否尝试先使用$first
将ng if与$first一起使用并显示复选框。bob=阿姨:)从你的帖子中不清楚firstItemOnly是什么。你能解释一下如果firstItemOnly检查的是什么吗?你可以在角度表达式中的三元运算符中完成整个表达式,比如在(firstItemOnly?(currentController.items中的项| orderBy:'-time'):(其他内容))