Only show first element of ng-repeat

后端 未结 4 1910
星月不相逢
星月不相逢 2020-12-31 09:58

How do I show only the first element in angular?

I\'m using ng-repeat like this:

相关标签:
4条回答
  • 2020-12-31 10:37

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

    <div>{{ products[0].price }}</div>
    
    0 讨论(0)
  • 2020-12-31 10:45

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

    <div>{{ products[products.length-1].price }}</div>
    
    0 讨论(0)
  • 2020-12-31 10:46

    You can use this:

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

    It will use the first element of the array.

    0 讨论(0)
  • 2020-12-31 11:01

    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
    
    0 讨论(0)
提交回复
热议问题