0

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?

1
  • 2
    Mandatory? No. Future proof to not use it? No. Use it. If your array grows, you'll thank yourself. Commented Sep 26, 2016 at 13:34

3 Answers 3

1

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

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.