1

Hi I'm a beginner at programming. I'm trying to put data from a json file into a variable

I want to have AngularJS put text into a html document like this

{{ data.text }}

And the text is based off of a json file I have in the app.js, so I set data using JQuery:

myApp.controller('Ctrl', function ($scope, $http) {
 $.getJSON("../static/file.json", function(data) {
   $scope.data = data;
 });

But nothing shows up initially. When I log $scope.data, it turns out to be undefined. But I know that my json file is correct because if I put all this code in some method that is called like

$scope.foo = function(){$.getJSON("../static/file.json", function(data) {
       $scope.data = data;
     });}

and have some button activate this, it'll work fine. But I want this to be there initially.

1
  • 2
    Why you use $.getJSON? Why not $http? Commented Jun 2, 2014 at 1:59

1 Answer 1

1

try

$.getJSON("/static/file.json", function(data) {
   $scope.data = data;
});

then it load file from web http://xxx/static/file.json, not file system.

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

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.