Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only show first element of ng-repeat

How do I show only the first element in angular?

I'm using ng-repeat like this:

<div ng-repeat="product in products" ng-show="$first">
    <div>{{ product.price }}</div>
</div>

But since I'm not repeating, then I shouldn't have to use ng-repeat? How can I get this to display just the first, without having to go in a ng-repeat?

like image 398
infinity Avatar asked Dec 03 '13 21:12

infinity


2 Answers

You might also want to use

<div ng-repeat="product in products|limitTo:1">
   <div>{{ product.price }}</div>
</div>

but yes the code below is better

<div>{{products[0].price}}</div
like image 161
nderoche Avatar answered Sep 25 '22 02:09

nderoche


Don't use ng-repeat directive, this should work:

<div>{{ products[0].price }}</div>
like image 34
Ufuk Hacıoğulları Avatar answered Sep 23 '22 02:09

Ufuk Hacıoğulları