简体   繁体   中英

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?

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

不要使用ng-repeat指令,这应该工作:

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

You can use this:

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

It will use the first element of the array.

或者,您可以通过执行以下操作显示数组中的最后一个:

<div>{{ products[products.length-1].price }}</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