Jason Jason - 7 months ago 29
PHP Question

get config settings from database to share across app in angular

I am trying to figure out the best practices way to retrieve my config settings from my database table and use these across my app. I am using PHP Slim for the RESTful API. I am not sure which is better? Use $rootScope or a service, and the best way to approach it?

Example using SLIM. If I want all the fields of the configs table I run this inside a controller:

Data.get('config').then(function(data){
configs = data.data;
});


currently I have a service but it is not working:

app.factory('dataShare', function($rootScope, $log, $http, Data) {

var configs = {};

Data.get('config').then(function(data){
configs = data.data;
});

return configs;

});


I need to be able to get any of these fields inside my postCtrl.

Answer

I changed a few things from the previous answer including showing how the print out should be called since were continuing the asynchronus behavior in to your controller.

  app.factory('dataShare', function($rootScope, $q, $log, $http, Data) {

      var configs= {};
      configs.getconfigs=function(){
          var deferred = $q.defer();
          Data.get('config').then(function(data){
            deferred.resolve(data.data);
          });
          return deferred.promise;
      }



    return configs;
    });

should then be able to call this inside your controller as long as you have q injected and the page this controller controls has

<script src= "https://cdnjs.cloudflare.com/ajax/libs/q.js/2.0.3/q.min.js"></script>

then call this.

dataShare.getconfigs().then(function(data){
    console.log(data);
});