Vicky Thakkar
Vicky Thakkar

Reputation: 17

Angular js: ng-repeat, Json string

My html code.

<div ng-repeat="text in collection">
   <h3>{{text.caption}}</h3>
</div>

My json string.

$scope.collection= [{"caption": "HELLO CAPTION 1","content":"SomeContent"}];

I have to use 'ng-repeat' in my html code even if there is only one key value pair. Is it mandatory to use ng-repeat for getting a single json string?

Upvotes: 0

Views: 197

Answers (3)

Karim
Karim

Reputation: 8632

is the array going to grow during the life of your application? if yes you should consider keep the ng-repeat as it is,

otherwise get the first element of the array and extract the field you need

<h3>{{collection[0].caption}}</h3>

Upvotes: 1

Murali
Murali

Reputation: 63

There is object inside the array so you specify the position

{{text[0].caption}} {{text[0].content}}

(or)

<div ng-repeat="text in collection">
    <div ng-repeat="object in text">
       <h3>{{object.caption}} {{object.content}}</h3>
    </div>

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222542

No need, if you have only one object inside an array you can use like this,

 <h3>{{collection[0].caption}}</h3>

DEMO

Upvotes: 1

Related Questions