Angular Js:如何在 ng-repeat 中找到第一个和最后一个元素并为它们添加特殊类? [英] Angular Js : How to find first and last element in ng-repeat and add special class to them?

查看:14
本文介绍了Angular Js:如何在 ng-repeat 中找到第一个和最后一个元素并为它们添加特殊类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 angular js 更改了以下内容

I changed the following using angular js

<section id="social">
    <h2 class="line">Social Profiles</h2>
    <a href="#" class="first"><i class="icon-facebook"></i></a>
    <a href="#"><i class="icon-twitter"></i></a>
    <a href="#"><i class="icon-linkedin"></i></a>
    <a href="#"><i class="icon-google-plus"></i></a>
    <a href="#" class="last"><i class="icon-rss"></i></a>
    <div class="clear"></div>
</section>

为此

<section id="social">
    <h2 class="line">Social Profiles</h2>
    <a ng-repeat="sMedia in socialMedia" ng-if="$first" href="#" >
        <i class="{{sMedia.icon}}"></i>
    </a>
    <div class="clear"></div>
</section>

一切似乎都很好,只是我找不到在 class="first" 的第一个元素中添加 class="last" 的方法代码>ng-repeat.

Everything seems fine except I could not find a way to add class="first" in the first element and class="last" in the last element of ng-repeat.

推荐答案

只需将 $first 和 $last 与 ng-class 结合使用:

Just use $first and $last combined with ng-class:

<a ng-repeat="sMedia in socialMedia" ng-class="{'first': $first, 'last': $last}" href="#" >

在此处查看它们的工作原理:https://docs.angularjs.org/api/ng/指令/ngRepeat

See here how they work: https://docs.angularjs.org/api/ng/directive/ngRepeat

这篇关于Angular Js:如何在 ng-repeat 中找到第一个和最后一个元素并为它们添加特殊类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆