I have a factory which loads datas from array, or a selected element from this array. But if I move it to an external .json file, I just get errors - i am newbie to angular in all but trying hard, keep in mind ^^ So if I use the simple
我有一个工厂从数组加载数据,或从该数组中选择一个元素。但是,如果我将它移动到外部.json文件,我只是得到错误 - 我是新手,除了努力,但仍然记住^^所以,如果我使用简单
$http.get('ports.json').success (function(data){
var works = data;
});
code, I get "ReferenceError: works is not defined". And if I try the
代码,我得到“ReferenceError:works not defined”。如果我试试
$http.get('ports.json').then((portRes){
var works = res.data;
});
I get the "Uncaught SyntaxError: Unexpected token {" error. But that code in not a factory, on a whole different page works, so no idea what can be wrong now :/ Here is the .json filet, and @ the link, you can check the plunker. Plunker - http://plnkr.co/edit/tMrJMjzc4pl7fQ1PIEiO?p=info
我得到“Uncaught SyntaxError:Unexpected token {”错误。但是那个代码不是工厂,在一个完全不同的页面上工作,所以不知道现在可能出现什么问题:/这里是.json文件,而@链接,你可以查看plunker。 Plunker - http://plnkr.co/edit/tMrJMjzc4pl7fQ1PIEiO?p=info
[
{
"Title": "Sprite",
"subTitle": "",
"Link": "sprite",
"Thumbnail": "img/portfolio02.png",
"Image": "img/ismont.png"
},
{
"Title": "Pepsi",
"subTitle": "Kristályvíz",
"Link": "pepsi",
"Thumbnail": "img/portfolio03.png",
"Image": "img/sanofimont.png"
}
]
EDIT: So I tried all what u wrote till now but nothing seemd to work... So I have thiw well-working factor now, which I want to transfer to an external .json file, to be very very clear for understand.
编辑:所以我尝试了所有你写的东西,但似乎没有什么工作......所以我现在有一个很好的工作因素,我想转移到外部.json文件,非常清楚明白。
portApp.factory("workFactory", function($http) {
var works = [
{
Title: "Sprite",
subTitle: "",
Link: "sprite",
Thumbnail: "img/portfolio02.png",
Image: "img/ismont.png"
},
{
Title: "Pepsi",
subTitle: "Kristályvíz",
Link: "pepsi",
Thumbnail: "img/portfolio03.png",
Image: "img/sanofimont.png"
}
];
return {
list: function() {
return works;
},
selected: function(detPath) {
selected = works.filter(function(work) {
return work.Link == detPath;
});
return selected;
}
};
4 个解决方案
#1
5
You have lot's of problems with the Plunker you posted.
你发布的Plunker有很多问题。
But the main problem was with this code:
但主要问题是这个代码:
portApp.factory("workFactory", function($http) {
$http.get('ports.json').then((portRes) {
var works = res.data;
});
return {
list: function() {
return works;
},
selected: function(detPath) {
selected = works.filter(function(work) {
return work.Link == detPath;
});
return selected;
}
};
});
Problems:
1. the then
method expects a function. Change (portRes)
to function(portRes)
.
2. Doing var works = res.data
creates a local var inside the callback function. You can't access it outside. Plus change res
to portRes
.
3. Returning the works
var wont work technically since it's a local var that isn't in the same scope. It wont work logically since the $http.get
is an async process and you have no idea when it will finish.
问题:1。then方法需要一个函数。将(portRes)更改为function(portRes)。 2.做var works = res.data在回调函数中创建一个局部var。你不能在外面访问它。另外将res更改为portRes。 3.返回工作var在技术上不起作用,因为它是不在同一范围内的局部变量。它不会在逻辑上工作,因为$ http.get是一个异步过程,你不知道什么时候会完成。
Edit:
After you edited, I believe you need something like this(I didn't check this so there may be some errors but that's the way to do things):
编辑:你编辑后,我相信你需要这样的东西(我没有检查这个,所以可能有一些错误,但这是做事的方式):
portApp.factory("workFactory", function($http) {
var state = {
works: null,
list: function() {
return this.works;
},
selected: function(detPath) {
return this.works.filter(function(work) {
return work.Link == detPath;
});
}
};
$http.get('myJson.json').success(function(data) {
state.works = data;
});
return state;
}
#2
0
You could use angular resource:
你可以使用角度资源:
angular.module('portApp').factory('Port', function ($resource) {
return $resource('ports.json');
});
#3
0
From another Stack Overflow thread:
从另一个Stack Overflow线程:
theApp.factory('mainInfo', function($http) {
var obj = {content:null};
$http.get('content.json').success(function(data) {
// you can do some processing here
obj.content = data;
});
return obj;
});
You can see it here:
AngularJS: factory $http.get JSON file
你可以在这里看到它:AngularJS:factory $ http.get JSON文件
#4
0
You forgot the function
keyword in the $http.get
and also the wrong variable name
您忘记了$ http.get中的function关键字以及错误的变量名称
$http.get('ports.json').then(function(res) { // <--- THIS
var works = res.data;
});
#1
5
You have lot's of problems with the Plunker you posted.
你发布的Plunker有很多问题。
But the main problem was with this code:
但主要问题是这个代码:
portApp.factory("workFactory", function($http) {
$http.get('ports.json').then((portRes) {
var works = res.data;
});
return {
list: function() {
return works;
},
selected: function(detPath) {
selected = works.filter(function(work) {
return work.Link == detPath;
});
return selected;
}
};
});
Problems:
1. the then
method expects a function. Change (portRes)
to function(portRes)
.
2. Doing var works = res.data
creates a local var inside the callback function. You can't access it outside. Plus change res
to portRes
.
3. Returning the works
var wont work technically since it's a local var that isn't in the same scope. It wont work logically since the $http.get
is an async process and you have no idea when it will finish.
问题:1。then方法需要一个函数。将(portRes)更改为function(portRes)。 2.做var works = res.data在回调函数中创建一个局部var。你不能在外面访问它。另外将res更改为portRes。 3.返回工作var在技术上不起作用,因为它是不在同一范围内的局部变量。它不会在逻辑上工作,因为$ http.get是一个异步过程,你不知道什么时候会完成。
Edit:
After you edited, I believe you need something like this(I didn't check this so there may be some errors but that's the way to do things):
编辑:你编辑后,我相信你需要这样的东西(我没有检查这个,所以可能有一些错误,但这是做事的方式):
portApp.factory("workFactory", function($http) {
var state = {
works: null,
list: function() {
return this.works;
},
selected: function(detPath) {
return this.works.filter(function(work) {
return work.Link == detPath;
});
}
};
$http.get('myJson.json').success(function(data) {
state.works = data;
});
return state;
}
#2
0
You could use angular resource:
你可以使用角度资源:
angular.module('portApp').factory('Port', function ($resource) {
return $resource('ports.json');
});
#3
0
From another Stack Overflow thread:
从另一个Stack Overflow线程:
theApp.factory('mainInfo', function($http) {
var obj = {content:null};
$http.get('content.json').success(function(data) {
// you can do some processing here
obj.content = data;
});
return obj;
});
You can see it here:
AngularJS: factory $http.get JSON file
你可以在这里看到它:AngularJS:factory $ http.get JSON文件
#4
0
You forgot the function
keyword in the $http.get
and also the wrong variable name
您忘记了$ http.get中的function关键字以及错误的变量名称
$http.get('ports.json').then(function(res) { // <--- THIS
var works = res.data;
});