gandev
gandev

Reputation: 131

How to display the first data in ng-repeat?

How to display the first data in ng-repeat

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

example sql SELECT TOP 1 * FROM Customers;

Upvotes: 0

Views: 212

Answers (4)

If you want to fetch only one record then use it like this. No need for ng-repeat.

Or still if you want to use, then use $index.

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

Upvotes: 2

Sebastianb
Sebastianb

Reputation: 2060

If you want only the first data, you shouldn't need ngRepeat, just reference it by its index images[0]:

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

Upvotes: 1

Łukasz
Łukasz

Reputation: 2171

Use $first variable:

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

See https://docs.angularjs.org/api/ng/directive/ngRepeat

Upvotes: 1

gaurav bhavsar
gaurav bhavsar

Reputation: 2043

Use ng-if="$index == 0"

See below

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

Upvotes: 2

Related Questions