使用 ng-if 选择 ng-repeat 循环中的第二个项目
Using ng-if to select second item in an ng-repeat loop
我正在开发一个页面,该页面循环浏览一堆内容并按天对它们进行排序/分组。分组有效,但我想将循环中的前两个项目与其他项目区别对待。我尝试使用ng-if="$first || $index == 1"
和ng-if="!$first || !$index != 1"
在下面的代码示例中使用ng-if
,但它似乎没有解决问题。
在ng-repeat
循环中选择第一个和第二个项目的正确方法是什么?
<div class="col-sm-12 day-block" ng-repeat="(key,value) in article.items | groupBy:'posted|date: MMM:dd' | toArray:true ">
<div class="col-sm-2 timestamp">
<h3>March 9</h3>
<h1>Day 4</h1>
</div>
<div class="col-sm-10" ng-repeat="article in value | orderBy:'posted' | unique: 'nid'">
<div class="col-sm-12">
<div class="row">
<div class="col-xs-6" ng-if="$first || $index == 1">
<span ng-class="blog-post-thumbnail" ng-bind-html="to_trusted(article.main_image)"></span>
<h2 ng-bind-html="to_trusted(article.node_title)"></h2>
<h4 ng-bind-html="to_trusted(article.author)"></h4>
<h4 ng-bind-html="to_trusted(article.posted)"></h4>
</div>
</div>
<div class="row" ng-if="!$first || $index != 1">
<div class="col-xs-4">
<span ng-class="blog-post-thumbnail" ng-bind-html="to_trusted(article.main_image)"></span>
</div>
<div class="col-xs-8">
<h3 ng-bind-html="to_trusted(article.node_title)"></h3>
<h4 ng-bind-html="to_trusted(article.author)"></h4>
</div>
</div>
</div>
</div>
</div>
您的表达式ng-if="!$first || !$index == 1
对于除 1 以外的所有索引都是正确的。要选择1st
和2nd
,您可以尝试:ng-if="$index == 0 || $index == 1"
对于其他人,您可以尝试ng-if="!($index == 0 || $index == 1)"
相关文章:
- 下拉选择可自动更改第二个下拉选择
- 为什么不'在JQuery中找到第二个css选择器的工作
- 当单击第一个李时,它显示内容一,当单击第二个李时,它显示内容二
- CORS-重定向到第二个GET正在接收的页面
- 正则表达式与数字中的第二个点匹配
- 如何在react js中移动第二个组件
- 使用Javascript,如何显示<tr>在另一<tr>悬停时,隐藏第二个<tr>
- 计算多个项目的价格
- 单击的第二个项目无法正常工作
- 无法为项目分配第二个项目
- JS深度比较2个对象并删除在第二个对象中找到的项目,而不是第一个
- 将最后一个元素移动到数组中的第二个项目的最快方法
- 将两个数组中的项目合并到第三个数组中,其中一列从第一个数组中减去第二个索引
- 使用 ng-if 选择 ng-repeat 循环中的第二个项目
- 为什么单击第二个项目后,此单击功能不起作用
- 按第一个列表中项目的索引将项目添加到第二个列表中
- 将单击下拉列表的项目传递到第二个文件
- jQuery -添加类与相同的类每第二个项目,然后重置,如果中断
- 引用 Visual Studio 中第二个项目中的泛型处理程序
- Ext.js: Vbox布局如何使第一个项目占用所有的空间时,第二个项目是隐藏的