Store JSON data into a variable Store JSON data into a variable json json

Store JSON data into a variable


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

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


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


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.