麻烦包括角度控制器中的JSON

时间:2022-10-26 23:17:11

I've got an angular controller containing a json object

我有一个包含json对象的角度控制器

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).

我正在使用这些数据使用ng-repeat迭代对象的数组(另外我的控制器中还有其他使用数据的函数)。

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

一切运行正常,直到我将json移动到它自己的文件中并使用$ http来包含它。

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'.

我在控制台中遇到的唯一错误来自我的所有其他功能,没有他们需要运行的数据,加上我的控制台日志中的“未定义”。所以我知道它会变成'.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;

我想也许我有错误的文件路径,尝试更改前缀与'../'从我的控制器上升一个目录。没运气。如果我把json放在同一个文件夹中然后写;

.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).

(我这样做是使用MAMP并且没有与尝试在本地获取文件相关的HTTP错误)。

Any help?

1 个解决方案

#1


2  

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

不确定您是否需要在此处使用$ http,为什么不在服务中包含您的数据,例如:

  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;
    ...
  }

#1


2  

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

不确定您是否需要在此处使用$ http,为什么不在服务中包含您的数据,例如:

  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;
    ...
  }