简体   繁体   中英

AngularJS: ng-repeat add some text with first element

I am using ng-repeat and would like to ask if we can add some extra text with first item?

 var items = [1,2,3,4,5,6,7,8,9]

 <div ng-repeat="item in items">
     {{item}}
 </div>

What i want is, it return me result like:

1 Hello
2
3
4
5
6
7
8
9

Thanks.

Take advantage of the angular $index variable, that gives you the index of the item in the collection:

<div ng-repeat="item in items">
     {{item}} <span ng-if="$index === 0">Hello</span>
</div>

or

<div ng-repeat="item in items">
     {{item}} <span ng-if="$first">Hello</span>
</div>

See documentation

You have the following special values:

  • $first
  • $middle
  • $last
  • $even
  • $odd

Can use a variety of special properties added to the child scope in ng-repeat and for this it includes $first along with $index, $last, $middle, $even, $odd

<div ng-repeat="item in items">
     {{item}} {{ $first ? 'I am first item' :''}}
 </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM