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?

Answers:

Answer

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

<div>{{ products[0].price }}</div>
Answer

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
Answer

You can use this:

<div ng-bind="products[0].price"></div>

It will use the first element of the array.

Answer

Alternately you can show the last in an array by doing this:

<div>{{ products[products.length-1].price }}</div>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.