0

Well here is my JSON

{ "questions" : {
                "Question 1" : [
                { "Q" :"Question" },
                { "A" : "Answer A"  },
                { "B" : "Answer B" },
                { "C" : "Answer C" },
                { "D" : "Answer D" },
                { "Answer" : "C" }
                ],
                "Question 2" : [
                { "Q" :"Question" },
                { "A" : "Answer A"  },
                { "B" : "Answer B" },
                { "C" : "Answer C" },
                { "D" : "Answer D" },
                { "Answer" : "C" }
                ] ,
         "Question 3":{

                 "x":"qwerty",
                 "y":"postdata"  }

}}

what I want to do is first display the names "Question 1", "Question "2 etc .. as links which is pretty easy to do with the help of

<div ng-repeat="(key,val) in questions" ><a href="#" > {{key}}</a> </div>

now based on the user click on those links ,i have to display the all details inside the array. please suggest how to do it . Thanks in advance .

2
  • Where do you want to display the details? Commented Jan 7, 2016 at 19:13
  • Can you post your js code? Commented Jan 7, 2016 at 19:13

4 Answers 4

1

i don't think you can go and use this JSON. First you should format it to be like array of question objects with Array.prototype.forEach (or map):

  [
    {
        name: 'Question 1',
        body: 'Foo', // here you use { "Q" :"Question" } value
        answers: [{ "answer" : "Answer A", "answerOption": "A" },
                  { "answer" : "Answer B", "answerOption": "B" },
                  { "answer" : "Answer C", "answerOption": "C" },
                  { "answer" : "Answer D", "answerOption": "D" }],
       correctAnswer: "C" // here you use info from { "Answer" : "C" }
      }
  ]

And then you can easily iterate. Here is the example

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

Comments

1

Change the JSON result to receive an object as a question configuration:

"Question 1" : [
    { 
     "Q" :"Question" ,
     "A" : "Answer A"  ,
     "B" : "Answer B" ,
     "C" : "Answer C" ,
     "D" : "Answer D" ,
     "Answer" : "C" 
    }
 ]

Iterates again in val array

 <div ng-repeat="(key,val) in questions" >
   <a href="#" ng-click="questionValues = val">{{key}}</a> 
 </div>
 <!-- Display information about question clicked -->
 <div ng-repeat="question in questionValues">
    <h1>{{question.Q}}</h1>
    <ol type="A">
        <li>{{question.A}}</li>
        <li>{{question.B}}</li>
        <li>{{question.C}}</li>
        <li>{{question.D}}</li>
    </ol>
 <div>

2 Comments

I've changed the JSON result and here is one example: jsfiddle.net/rokoala/hb2ynuqq
I've deleted my comment regarding the scope of questionValues possibly needing to be declared in scope since your (excellent) linked example gets around it using as. :-)
0

As long as your variable representing that JSON is on the $scope, you should be roughly in the right place for this to work.

$scope.questions = {
    "Question 1" : [
        { "Q" :"Question" },
        { "A" : "Answer A"  },
        { "B" : "Answer B" },
        { "C" : "Answer C" },
        { "D" : "Answer D" },
        { "Answer" : "C" }
        ],
    "Question 2" : [
        { "Q" :"Question" },
        { "A" : "Answer A"  },
        { "B" : "Answer B" },
        { "C" : "Answer C" },
        { "D" : "Answer D" },
        { "Answer" : "C" }
        ] ,
    "Question 3": [
        { "x":"qwerty" },
        { "y":"postdata" }  
    ]
}

Note, on your Question 3, I changed it to a list of key-value pairs as in your other two.

Now your key-value reference in HTML should build out the list of links for Question 1, 2, etc. as you're expecting. Having those links then display the contents of val however when you click a link will take more code and clarification from you on what you were aiming to do, visually.

If you can post your other JS (your controller, or wherever you're doing this) that would be helpful.

Comments

0

Thank you all for your answers . I changed my json as suggested by Denis and it worked for me.

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.