将JSON数据存储到变量中

时间:2015-05-22 10:34:09

标签: json angularjs factory

我是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 
    }
];

我也试过了this question

3 个答案:

答案 0 :(得分:3)

您可以使用$http来读取json文件。 E.g。

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

答案 1 :(得分: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;
}); 

答案 2 :(得分:0)

如果我想在控制器内部的某个特定请求上将JSON数据添加到工厂变量是你的意思,那么$http是最好的:

     appmodule.controller('appCtrl',   ['$http','quizFactory',function($http,quizFactory){
        $http('somefile.json').success(function(data){
           quizFactory.questions=data;  
        }
     })

请注意,你必须依赖注入工厂,是的,没有必要手动解析数据,Angular会自动关注它。