Show json using AngularJS
I'm new to AngularJS. Now I have a json object from my spring controller, how can I use / print in my jsp?
I've tried something like this. the console shows json fine, but with angular it doesn't ...
<div data-ng-init="stats=${stats}">
<li data-ng-repeat="stat in stats">{{stat.name}}</li>
</div>
<script>
console.log(${stats});
</script>
Json:
{ "Types": [{"name": "study", "value":0},{"name": "health", "value":0},{"name": "culture", "value":0},{"name": "nightlife", "value":0},{"name": "other", "value":0},{"name": "friendship", "value":0}] })
+3
Joris chen
source
to share
2 answers
Since the JSON string needs to be quoted correctly if put into the value of the HTML attribute, you are either avoiding "
characters in the JSON, or perhaps better use quotes '
for ngInit
:
<div data-ng-init='stats=${stats}'>
<li data-ng-repeat="stat in stats.Types">{{stat.name}}</li>
</div>
Demo: http://plnkr.co/edit/XLIE9VCSn9gL3oO6ULol?p=info
+2
dfsq
source
to share
You need to specify an array property stats
.
<li data-ng-repeat="stat in stats.Types">{{stat.name}}</li>
+2
cgTag
source
to share