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>

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?



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

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

You might also want to use

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

but yes the code below is better


You can use this:

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

It will use the first element of the array.


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

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


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.