5

I am new to AngularJS. I just want to load the JSON file data into a variable that is located in factory.

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;
            }
        }
    };
});

The above code is stored in app.js file and my JSON file is same as the above.

[


{
        "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 
    }
];

I have tried this question too.

3 Answers 3

3

You can use $http to read json file. E.g.

$http.get('someFile.json').success(function(data) {    
        questions = data;
    });    
Sign up to request clarification or add additional context in comments.

6 Comments

I replaced my JS file from above with myApp.factory('quizFactory',['$http', function ($http) { $http.get('ques.json').success(function(data) { var questions = data; }); }); but still not working it is unable to load the json file
It should work, may be your json file is not in the correct place. Here is the working demo plnkr.co/edit/PpRvBhbZ9LHT8EQeUV2a?p=preview
In the app.js file if I change app.controller to app.factory it is not working
Sorry for asking so many questions can u help where should i add the controller data in this plnkr.co/edit/fOJoCVuIksmD3LSnGqBt?p=catalogue myApp.directive file
I would be glad to help :) , could you please post a new question describing what's the problem
|
0

You can redefine your factory like this :

  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;


}
]);

and call it from your controller like this :

     $scope.objectsArray = [];

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

2 Comments

why need forEach loop? instead you can directly store data into array object.
Edited just now in my case i was doing some extra processing before adding it to the array .
0

If I want to add the JSON data to the factory variable on the some particular request inside a controller is what you meant, then $http is the preferable one:

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

Here note that you have to dependency inject the factory, and yes, it is not necessary to manually parse the data, Angular will automatically look after it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.