加载JSON数据进厂文件 [英] load json data into the factory file
问题描述
我是新来angularJS刚开始学习它。是否有人可以帮助我这个例子。我怎样才能加载JSON文件到的script.js文件,而无需直接将整个数据。
下面是该程序的code:
I am new to angularJS and just started learning it. Can someone please help me with this example. How can I load the json file into the script.js file without directly adding the entire data. Here is the code of the program:
enter code here
推荐答案
您需要正确的 ques.json
粘贴的JSON(通过使用引号,
),否则如果你将调用 $ http.get()
那么你将得到异常由于无效的JSON。
You need to correct the json pasted in ques.json
(by using quotes ""
) otherwise if you will invoke $http.get()
then you will get exception due to invalid json.
一旦你将纠正在JSON内容 ques.json
那么就使用 $ HTTP
并分配结果问题的变量。
Once you will correct the json content in ques.json
then just use $http
and assign the result to questions variable.
例如
app.factory('quizFactory', function($http) {
var questions = [];
$http.get('ques.json').success(function(data) {
questions = data;
})
return {
getQuestion: function(id) {
if(id < questions.length) {
return questions[id];
} else {
return false;
}
}
};
});
下面是演示
这篇关于加载JSON数据进厂文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!