1

I've got an angular controller containing a json object

app.controller("buildControl", function($scope){

$scope.buildData = [
{
    'title':'Workspace',
    'options': [{
        'title': 'Studio'
    },{
        'title': 'Garage'
    },{
        'title': 'Blank'
    }]
},{
    'title':'Frame',
    'options': [{
        'title': 'Studio'
    },{
        'title': 'Garage'
    },{
        'title': 'Blank'
    }]
}]
});

I'm using this data to iterate the object's arrays using ng-repeat (plus I have other functions in my controller that use the data).

Everything runs fine until I moved the json out into it's own file and used $http to include it.

app.controller("buildControl",function($http, $scope){

$http.get("json/buildData.js")
  .success(function(response){
    $scope.buildData = response.data;
  }).error(function(error){
    console.log(error);
  });
});

The only errors I get in my console are from all my other functions breaking without the data they need to run, plus an 'undefined' from my console log. So I know that it's going to '.error'.

I figured maybe I had the filepath wrong, have tried changing prefixing it with '../' to go up a directory out of my controllers. No luck. If I put the json in the same folder and just write;

.get("buildData.js")

I get this as the error

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /buildData.js was not found on this server.</p>
</body></html>

But then I sort out the filepath and it goes back to undefined. So I don't think this is the problem.

(I'm doing this using MAMP and having no HTTP errors associated with trying to get files locally).

Any help?

6
  • 1
    Before the line ` $scope.buildData = response.data;` add alert(typeof response.data) and see if it is a string or object. If it is a string, then it's raw JSON that must be parsed first. Commented Jun 5, 2016 at 19:58
  • Why make it a .js file? Why not make it a true .json file? Commented Jun 5, 2016 at 20:00
  • I have also changed the file extension to and from .json, no dice. Commented Jun 5, 2016 at 20:11
  • Jeremy, adding this here does not work because the .success function isn't running so no data is being sent over and the alert isn't being run Commented Jun 5, 2016 at 20:12
  • Path is clearly wrong if you get 404 status returned from server Commented Jun 5, 2016 at 21:42

1 Answer 1

2

Not sure if you need to use $http here, why not include your data from a service, something like:

  angular
    .module('MyApp')
    .service('buildData', buildData);

  function buildData() {
    return [{
      'title':'Workspace',
      'options': [{
         'title': 'Studio'
      },{
        'title': 'Garage'
      },{
        'title': 'Blank'
      }]
    ...
    }]
  };

Your controller would look something like this:

.controller('buildControl', buildControl);

  buildControl.$inject = ['$scope', 'buildData'];
  function buildControl($scope, buildData) {
    $scope.buildData = buildData;
    ...
  }
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.