这是我的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" }
}}

我想要做的是首先显示名称"问题1","问题" 2等...作为链接,这很容易做到的帮助
<div ng-repeat="(key,val) in questions" ><a href="#" > {{key}}</a> </div>
&#13;
现在基于用户点击这些链接,我必须显示数组内的所有细节。请建议怎么做。 提前谢谢。
答案 0 :(得分:1)
更改JSON结果以将对象作为问题配置接收:
"Question 1" : [
{
"Q" :"Question" ,
"A" : "Answer A" ,
"B" : "Answer B" ,
"C" : "Answer C" ,
"D" : "Answer D" ,
"Answer" : "C"
}
]
在val数组中再次迭代
<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>
答案 1 :(得分:1)
我认为你不能使用这个JSON。首先,您应该将其格式化为具有Array.prototype.forEach
(或地图)的问题对象数组:
[
{
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" }
}
]
然后你可以轻松迭代。 Here就是示例
答案 2 :(得分:0)
只要表示JSON的变量位于$ scope上,您就应该大致在适当的位置工作。
$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" }
]
}
请注意,在您的问题3中,我将其更改为键值对列表,与其他两个一样。
现在,HTML中的键值引用应该构建问题1,2等的链接列表,正如您所期望的那样。拥有这些链接然后显示val
的内容,但是当您点击链接时,您将需要更多代码并澄清您的目标,视觉上。
如果您可以发布您的其他JS(您的控制器,或者您正在执行此操作的任何地方),那将会有所帮助。
答案 3 :(得分:0)
谢谢大家的回答。我按照丹尼斯的建议改变了我的json,这对我有用。