Setting a constant through a promise in the startup phase - angular

Is it possible to set a constant at the startup stage? I will need this value for my services to work correctly, this is my case.

 angular
 .constant('MYCONSTANT', {})
 .factory('myFactory', function($http) {
     return {
         getSomething : getSomething
     };

     function getSomething() {
         return $http.get('myurl');
     }
 })
 .run(function(myFactory, MYCONSTANT) {
     myFactory.getSomething().then(function(response) {
         MYCONSTANT = response;
     });
 });

If I console the value of a constant in the execution phase, it has the correct value, however, if I check it for any other service when they are called, this is an empty object.

I would not want to rely on a resolution method opened with ui-router.

+4
source share
1 answer
      MYCONSTANT = response;

just assigns a new value to a local variable MYCONSTANT, in which case the original reference to the object is lost. For the intended purpose, it must be

      angular.forEach(MYCONSTANT, function (val, key) {
        // if MYCONSTANT is non-empty
        delete MYCONSTANT[key];
      });
      angular.extend(MYCONSTANT, response);
+4
source

All Articles