Angular Js : How to find first and last element in

2019-04-03 05:43发布

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>

To this

<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>

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.

2条回答
冷血范
2楼-- · 2019-04-03 06:21

You can use:

#social a:first-child{ /* your first css code */ }
#social a:last-child{ /* your last css code */ }
查看更多
劫难
3楼-- · 2019-04-03 06:24

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

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

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

查看更多
登录 后发表回答