我有一个json文件,我希望在我的角应用程序中提供其中的内容。我正在阅读in this thread关于使用$ http.get从文件系统加载json的问题。我无法让这个工作,我不断得到" http://localhost:9000/jsonFile.json找不到"当我运行在控制台中加载它的代码时(通过$ injector.get)。我使用yeoman来生成我的应用程序,结构如下所示:
app
--scripts
--services
--jsonLoader.js
--app.js
--index.html
--jsonFile.json
jsonLoader.js文件是
angular.module('jsonLoader', [])
.service('loader', function($http){
var schema = {};
$http.get('jsonFile.json')
.then(function(res){
schema = res.data;
});
return schema;
});
我已经尝试了可能引用json文件位置的每个路径组合,但是当我尝试访问该服务时,我仍然获得了404。任何帮助都将非常感激!
答案 0 :(得分:1)
您的问题与Angular无关,但很可能是您的Grunt设置。 您的Grunt没有观看JSON文件,因此您的Angular应用程序无法访问您尝试访问的JSON文件。
要使JSON文件可访问,您需要将以下内容添加到您的grunt文件中,该文件是Yeoman for Angular的一部分:
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'<%= yeoman.app %>/{,*/}*.html',
'<%= yeoman.app %>/{,*/}*.json', //added this line
'.tmp/styles/{,*/}*.css',
'<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
]