angular ng-repeat set last element style

本文介绍了如何在AngularJS中利用ng-class指令结合三元运算符为列表项动态添加样式,包括直接在HTML中使用三元表达式及通过控制器中的函数实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[html] view plain copy 在CODE上查看代码片派生到我的代码片

        <li ng-repeat="one in many">  
                  {{one.name}}  
        </li>  

那么我就可以这样加
[html] view plain copy 在CODE上查看代码片派生到我的代码片

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

还有一种方法就是使用js计算的方法
[html] view plain copy 在CODE上查看代码片派生到我的代码片

<li ng-repeat="one in many" ng-class="isLast($last)">  
  {{one.name}}  
</li>  

Controller中的代码定义是这样
[javascript] view plain copy 在CODE上查看代码片派生到我的代码片

$scope.isLast = function(flag) {  
    return flag ? 'item-last' : 'item-not-last';  
};  

css定义是这样
[css] view plain copy 在CODE上查看代码片派生到我的代码片

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

对,这就是一个三元运算,你可以直接在html里面用,像这样
[html] view plain copy 在CODE上查看代码片派生到我的代码片

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值