JSON array defined in scope:
$scope.faq = [
{"Question 1": "Answer1"},
{"Question 2": "Answer2"}
];
HTML:
<div ng-repeat="f in faq">
{{f}}
</div>
Output:
{"Question 1": "Answer1"}
{"Question 2": "Answer2"}
What I want output to look like:
Question 1 - Answer1
Question 2 - Answer2
How it seems like it should work:
<div ng-repeat="f in faq">
{{f.key}}-{{f.value}}
</div>
... but it doesn't.
Change your json array in scope like;
$scope.faq = [
{key: "Question 1",
value: "Answer1"},
{key: "Question 2",
value: "Answer2"}
];
And in your view;
<div ng-repeat="f in faq">
{{f.key}}-{{f.value}}
</div>
Due to it being within an array, you will have to loop through the key values of each object.
http://fiddle.jshell.net/TheSharpieOne/QuCCk/
<div ng-repeat="value in faq">
<div ng-repeat="(question,answer) in value">
{{question}} - {{answer}}
</div>
</div>
Alternately:
If you have just a simple object:
$scope.faq = {
"Question 1": "Answer1",
"Question 2": "Answer2"
};
You could avoid the second repeat
<div data-ng-repeat="(question,answer) in faq">
{{question}} - {{answer}}
</div>
http://fiddle.jshell.net/TheSharpieOne/D3sED/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With