angularjs中使用ng-repeat渲染最后一个li的时候设置不同样式

来源:互联网 发布:知乎 永久封号 编辑:程序博客网 时间:2024/06/08 07:40

如题所示,比如我要在下面的代码的最后一个li节点添加一个样式

<li ng-repeat="one in many">  {{one.name}}</li>
那么我就可以这样加

<li ng-repeat="one in many" ng-class="{'item-last':$last}">  {{one.name}}</li>

还有一种方法就是使用js计算的方法

<li ng-repeat="one in many" ng-class="isLast($last)">  {{one.name}}</li>
Controller中的代码定义是这样

$scope.isLast = function(flag) {    return flag ? 'item-last' : 'item-not-last';};
css定义是这样

.item-last { /* Your Styles */ }.item-not-last { /* Your Styles */ }

对,这就是一个三元运算,你可以直接在html里面用,像这样

<li ng-repeat="one in many" ng-class="$last ? 'item-last' : 'item-not-last'">  {{one.name}}</li>

参考文章:

Different class for the last element in ng-repeat







0 0