Suresh B
Suresh B

Reputation: 1652

How to return array of object use json file in angularjs factory?

Here I created sample for services call which is Working fine Call one by one Link for sample.

like this:-

     JSONService.getJSON('file.json').then(function(data){
       $scope.languages = data;
  });

 JSONService.getJSON('file1.json').then(function(data){
       $scope.languages1 = data;
  });

I don't want to send separately. why because if its only two three call means fine.but I need to do more than 100 calls. that time I can't do one by one. so tried like this

      JSONService.getJSON('file.json,file1.json').then(function(data){
       $scope.languages = data[0];
       $scope.languages1 = data[1];
  });

In services use split the values and try to push the promise one by one and return as array its not working I don't know where i did mistake can any one help me on this..

app.service('JSONService', function($http){  
   var data = [];
    return{
        getJSON: function(url){
          var parameter=url.split(',');
          for(var i=0; i< parameter.length; i++){
             $http.get(parameter[i])
                .then(function(response){
                    data.push(response);
                });
          }
          return data;
        }
    };
 });

Link for Sample 2 not working

Upvotes: 0

Views: 527

Answers (2)

sudo
sudo

Reputation: 323

$q

This is what $q is for.

app.service("JSONService", ["$http", "$q", function($http, $q){
    return {
        getJson: function(callback){
            _urls = ["data1.json", "data2.json", "data3.json"],
            _urlCalls = [],
            _data = [];

            angular.forEach(_urls, function(url) {
                _urlCalls.push($http.get(url));
            });

            $q.all(_urlCalls).then(
                function(results) {
                    results.forEach(function(e,i){
                        _data.push(e.data);
                    });
                    callback(_data);
                 },
                 function(errors) {},
                 function(updates) {}
            );
        }
    }
}]);

Usage

From the controller.

JSONService.getJson(function(response){
    console.log(response);
}

Upvotes: 0

Narendra CM
Narendra CM

Reputation: 1426

you need to use promises for that. here is the service you need to use

app.service('JSONService', function($http, $q){  
   var data = [];
    return{
        getJSON: function(url){
          var urls = url.split(','),
              promises = [];
          for(var i=0; i< urls.length; i++){
             var inPromise  = $http.get(urls[i])
                .then(function(response){
                    data.push(response.data);
                });
              promises.push(inPromise);
          }
        //return the promise from the $q.all, that makes sure, that all pushed promises are ready and return the chapters.
        return $q.all(promises).then(function () {
            return data;
        });

        }
    };
 });

Also updated your plnkr http://plnkr.co/edit/smMv9jPyMRNYX2AzAuRC?p=preview. This concatenates all the result to one array object languages

Upvotes: 1

Related Questions