1

Well, I begin...

I've already a factory that loads the data from JSON files succefully:

FIRST FACTORY:

statisticsModule.factory('globalFactory', ['$http', function($http){
  return $http.get('../statistics/php/config_statistics.json')
  .success(function(data){
    return data;
  })
  .error(function(err){
    return err;
  });
}]);

I want:

  1. Read a specific URL that contains this JSON
  2. Inject factory inside another factory that read the data for the URL above.

This factory load the data and returns by console that I want:

SECOND FACTORY:

statisticsModule.factory('statusFactory', ['globalFactory', '$http', function(globalFactory, $http){

  return globalFactory.success(function(data){

      var deserialize = angular.fromJson(data.config.graph_conf_array.arrayReportBD);
      var statusUrl = deserialize[0];

      $http.get(statusUrl).success(function(data){
        console.log(data);
      }).error(function(err){
       err;
      });

    });

}]);

But I want a third task:

  1. Inject data in controller

    This is my controller:

statisticsModule.controller('tableController', ['statusFactory', '$scope', '$http', function(statusFactory, $scope, $http){

statusFactory.success(function(){
  
});

}]);

If I want loads data from Second Factory in my controller, How I do?

Sorry for bad english, and thanks in advance.

1 Answers1

0

First of all don't use .success as it doesn't help in promise chaining. .success doesn't create a new promise it returns the original promise.

First Factory

statisticsModule.factory('globalFactory', ['$http', function($http){
  return $http.get('../statistics/php/config_statistics.json')
   .then(function(response){
     return response;
   }, function (error) {
     return error. 
   })
}]);

Second Factory

statisticsModule.factory('statusFactory', ['globalFactory', '$http', function(globalFactory, $http){

   return globalFactory.then(function(data){
        var deserialize = angular.fromJson(data.config.graph_conf_array.arrayReportBD);
        var statusUrl = deserialize[0];
        return $http.get(statusUrl);
   }).then(function (data) {
        return data;
   });

}]);

Now in your Controller

statisticsModule.controller('tableController', ['statusFactory', '$scope', '$http', function(statusFactory, $scope, $http){

    statusFactory.then(function(data){   // data is the data received after get call to statusUrl
        // do something
    });

}]);