Я новичок в AngularJS. Я просто хочу загрузить данные файла JSON в переменную, которая находится на заводе.Хранить данные JSON в переменной
myApp.factory('quizFactory', function() {
var questions = [
{
"setId":1,
"question":"What is the value of the 7 in the following number? 850,765",
"options": ["70", "7", "7000", "700"],
"answer": 3
},
{
"setId":2,
"question":"Is 7 & 9 even or odd?",
"options": ["Even", "Odd", "Can't Say", "Dont know"],
"answer": 1
},
{
"setId":3,
"question":"In the number 5,281,946 what is the value of the 3rd place?",
"options": ["100", "10,000", "1,000,000", "1,000"],
"answer": 0
},
{
"setId":4,
"question":"Is 12 + 50 even or odd?",
"options": ["Even", "Odd", "Can't Say", "Dont know"],
"answer": 0
},
{
"setId":5,
"question":"What does the 3 represent in the number below? 3051",
"options": ["3", "30", "300", "3000"],
"answer": 3
}
];
return {
getQuestion: function(id) {
if(id < questions.length) {
return questions[id];
} else {
return false;
}
}
};
});
Приведенный выше код хранится в файле app.js, а мой JSON-файл аналогичен указанному выше.
[
{
"setId":1,
"question":"What is the value of the 7 in the following number? 850,765",
"options": ["70", "7", "7000", "700"],
"answer": 3
},
{
"setId":2,
"question":"Is 7 & 9 even or odd?",
"options": ["Even", "Odd", "Can't Say", "Dont know"],
"answer": 1
},
{
"setId":3,
"question":"In the number 5,281,946 what is the value of the 3rd place?",
"options": ["100", "10,000", "1,000,000", "1,000"],
"answer": 0
},
{
"setId":4,
"question":"Is 12 + 50 even or odd?",
"options": ["Even", "Odd", "Can't Say", "Dont know"],
"answer": 0
},
{
"setId":5,
"question":"What does the 3 represent in the number below? 3051",
"options": ["3", "30", "300", "3000"],
"answer": 3
}
];
Я пробовал this question тоже.
Я заменил свой файл JS сверху myApp.factory ('quizFactory', ['$ http', function ($ http) { \t $ http.get ('ques.json'). Success (function (data) { var questions = data; }); }); но все еще не работает, он не может загрузить json-файл –
Он должен работать, может быть, ваш json-файл не в нужном месте. Вот рабочая демонстрация http://plnkr.co/edit/PpRvBhbZ9LHT8EQeUV2a?p=preview – sol4me
В файле app.js, если я меняю app.controller на app.factory, он не работает. –