0

Usually Angular get from HTTP request the JSON from server side (like Django). But, to accelerate rendering, would like to write down on server side the JSON into a Javascript VAR and let proceed Angular on this javascript variable containing the JSON.

My question are: 1) How to pass this javascript var to angular $scope variable ? (without HTTP).

2) Is writing down the JSON into the HTML a bad/good practice ? (given my web app is fairly static).

Kim Kern
  • 54,283
  • 17
  • 197
  • 195
tensor
  • 3,088
  • 8
  • 37
  • 71
  • Possible duplicate of [Returning JSON array from a Django view to a template](http://stackoverflow.com/questions/15592811/returning-json-array-from-a-django-view-to-a-template) – creimers May 03 '17 at 16:52
  • This is more of an Angular question. When I have the Javascript VAR, how to pass it into scope ? – tensor May 04 '17 at 01:07
  • Check out this post: https://stackoverflow.com/questions/19383725/how-to-access-global-js-variable-in-angularjs-directive – creimers May 04 '17 at 05:43

1 Answers1

0

You can do a simple $scope = {{ some_django_tag|safe }}. I can't see why it would be a bad practice if you're 100% sure that the output will be valid JS. I actually do this a bit to instantiate the scope on the page load.

somecallitblues
  • 680
  • 4
  • 9