0

I'm testing out angular for the first time. In javascript I have a json-object. How do I make it available to use in my angular template?

1
  • 2
    Angular use regular objects. Assigning it to a scope property will work out of the box. You should have a look at AngularJS Tutorial and Developer Guide. Commented Aug 21, 2013 at 12:43

2 Answers 2

1

Firstly your json-object needs to be in the scope of a controller:

$scope.obj = /* some data */

And inside the HTML, simply refer to it as obj. For example to iterate over it:

<div ng-repeat="(key,value) in obj"></div>
Sign up to request clarification or add additional context in comments.

Comments

0

Angular comes with a angular.fromJson(jsonData) method that you can use to transform your JSONdata into a JavaScript object.

See: http://docs.angularjs.org/api/angular.fromJson

Then you just need to expose it on a scope like:

$scope.myData = angular.fromJson(jsonData);

Now you are free to use it in your templates:

<div>{{myData.someProperty}}</div>

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.