如何在ng-repeat中显示第一个数据?

时间:2016-11-21 18:41:47

标签: angularjs

如何以ng-repeat显示第一个数据

<div class = "panel-body">
<div ng-repeat=" img in images">
    <img src="http://photo/{{img.path}}" alt="">
</div>
Panel content

示例sql SELECT TOP 1 * FROM Customers;

4 个答案:

答案 0 :(得分:2)

使用ng-if="$index == 0"

见下文

<img ng-if="$index == 0" ng-src="http://photo/{{img.path}}" alt="" />

答案 1 :(得分:2)

如果您只想获取一条记录,请像这样使用它。不需要ng-repeat。

或者如果你想使用,仍然使用$ index。

<div class = "panel-body">
  <div ng-repeat=" img in images">
    <img src="http://photo/{{images[0].path}}" alt="">
 </div>

答案 2 :(得分:1)

如果您只想要第一个数据,则不需要ngRepeat,只需通过索引images[0]引用它:

<img ng-src="http://photo/{{images[0].path}}" alt="">

答案 3 :(得分:1)

使用$first变量:

<img ng-if="$first" ng-src="http://photo/{{img.path}}" alt="" />

请参阅https://docs.angularjs.org/api/ng/directive/ngRepeat