3

Im new to AngularJS. Now I have a json object from my spring controller, how do i use/print in my jsp?

I tried something like this. the console shows the json perfectely, and with angular it does not...

<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}] })
6
  • 3
    show your json as well. Commented May 29, 2015 at 21:01
  • I have put my json in my question Commented May 29, 2015 at 21:14
  • 1
    It should probably be: data-ng-repeat="stat in stats.Types". Commented May 29, 2015 at 21:15
  • You should use data-ng-repeat="stat in stats.Types" Commented May 29, 2015 at 21:16
  • 1
    What's displayed there isn't valid JSON, or even JavaScript which is causing angular to choke. Commented May 29, 2015 at 21:26

2 Answers 2

2

Because JSON string must be properly quoted if placed into HTML attribure value, you either escape " characters in JSON or probably 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

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

Comments

2

You need to reference the array property of stats.

<li data-ng-repeat="stat in stats.Types">{{stat.name}}</li>

2 Comments

Was it <div data-ng-init="stats=${stats}"> generating error ?
@Vineet I think dfsq answer addresses that problem.

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.