将JSON数据存储到变量中

5

我是AngularJS的新手,我只想将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 
    }
];

我也尝试了这个问题

3个回答

3

您可以使用$http读取JSON文件。例如:

$http.get('someFile.json').success(function(data) {    
        questions = data;
    });    

我用以下代码替换了我的JS文件:myApp.factory('quizFactory',['$http', function ($http) { $http.get('ques.json').success(function(data) {
var questions = data; }); }]);但仍然无法正常工作,它无法加载JSON文件。
- Ramana Uday
它应该可以工作,也许你的JSON文件没有放在正确的位置。这是一个可工作的演示:http://plnkr.co/edit/PpRvBhbZ9LHT8EQeUV2a?p=preview - sol4me
在app.js文件中,如果我将app.controller更改为app.factory,它不起作用。 - Ramana Uday
抱歉问了这么多问题,您能帮忙看一下在这个http://plnkr.co/edit/fOJoCVuIksmD3LSnGqBt?p=catalogue中应该在哪里添加控制器数据到myApp.directive文件吗? - Ramana Uday
我很乐意帮忙 :) ,您可以发一篇新问题描述一下具体问题吗? - sol4me
嗨@sol4me,这是实际的问题,链接在此:http://stackoverflow.com/questions/30396533/load-json-data-into-the-factory-file - Ramana Uday

0
你可以像这样重新定义你的工厂:
  angular.module('yourmodule')
 .factory('jsonResourceService', ['$http',
function($http) {
  var jsonResourceService = {};

  jsonResourceService.load = function(callback) {
    $http.get('path/data.json').success(function(data) {
      callback(data);
    });
   return jsonResourceService;


}
]);

然后在您的控制器中像这样调用它:

     $scope.objectsArray = [];

  jsonResourceService.load(function(data) {
 $scope.objectsArray;
}); 

为什么需要使用forEach循环?你可以直接将数据存储到数组对象中。 - Hardy
刚刚编辑了,我的情况是在将其添加到数组之前进行了一些额外的处理。 - Master Mind

0
如果我想在控制器内的某个特定请求中将JSON数据添加到工厂变量中,那么你所说的最好使用$http。
     appmodule.controller('appCtrl',   ['$http','quizFactory',function($http,quizFactory){
        $http('somefile.json').success(function(data){
           quizFactory.questions=data;  
        }
     })

在这里请注意,您必须进行依赖注入工厂,是的,手动解析数据并不是必要的,Angular会自动处理它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接