1

I have a large JSON file structured like the following which I'm trying to loop over in Angular:

    {
       "subject1":[
          {
             "title":"titlehere",
             "info":"infohere."
          }],

       "subject2":[
          {
             "title":"titlehere",
             "info":"infohere."
          }],
       "subject3":[
          {
             "title":"titlehere",
             "info":"infohere."
          }]
}

I want my page to grab the key for each 'category' and then the display the title underneath it. I'm able to get the key to display but I can't seem to figure out how to grab the string for each title. This is what I have in my HTML:

            <div ng-repeat="(key, value) in faqs">
            <h3>{{ key }}</h3>
              <ul>
                  <li><a href="#" ng-click="showHide(pageInfo)">{{ value }}</a></li>
              </ul>
            </div>

I'm not sure what I should be using instead of {{ value }} which grabs nothing but the entire JSON file as a string. I tried {{ value.title }} and still had no luck.

Any suggestions?

2
  • 1
    How do you know it's "pure JSON"? Have you checked what (typeof value) says? Commented Jul 13, 2015 at 9:44
  • I was using that as a way to describe that it's pulling the entire json object as a string. Commented Jul 13, 2015 at 9:49

1 Answer 1

3

Use {{ value[0].title }}

your code be

<div ng-repeat="(key, value) in faqs">
     <h3>{{ key }}</h3>
     <ul>
        <li ng-repeat="val in value"><a href="#" ng-click="showHide(pageInfo)">{{ val.title }}</a></li>
     </ul>
</div>
Sign up to request clarification or add additional context in comments.

5 Comments

While this works, that only grabs the first one instead of everything. If there were 100 titles how would I grab all of them?
Again you loop again towards ul that solve, I update it wait a min
@JamesIves Updated check
Works great. Not sure why I didn't think of that! Thank you.
@JqueryKing: Doesnt the second ng-repeat simply loops the entire ul?

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.