Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to override module values/constants in angularJS

I have written a module in angularJS that encapsulates all the backend communications. For greater flexibility I have the api prefix as a constant value on the module (could be value since I am not using it in the config phase). so something like

angular.module('myapp.data').constant('apiPrefix', '/api/data');

Now I want to use this module from two different applications. One uses /api1/data and the other one /api2/data and I would like to change this during the config phase of the application. I know how to do that with a provider, but having a provider to hold a value seems like an overkill to me. Can I modify used modules constants or values from the application config phase?

something like:

angular.module("data", [])
.value('apiPrefix', '/api/data')
.factory('display', function(apiPrefix){
  return {
    pref: function(){
      console.log(apiPrefix);
      return apiPrefix;
    }
  }
});


angular.module("myApp",['data'])
.config(['apiPrefix', function(prefix){
  prefix = 'https:/api/data'; 
}])
.controller("Example", function($scope, display) {
   $scope.prefix = display.pref;
});
like image 711
masimplo Avatar asked Apr 09 '14 15:04

masimplo


2 Answers

to override the module values, you can redefine the angular value in later modules. I believe it should not be done module config time.

angular.module("data", [])
.value('apiPrefix', '/api/data')
.factory('Display', function(apiPrefix){
  return {
    pref: function(){
      return apiPrefix;
    }
  }
});




angular.module('myapp', ['data'])
  .value('apiPrefix', '/api2/data')
  .controller('MainCtrl', function($scope, Display)    {
      $scope.name = Display.pref();
  });

see the plunker here: http://plnkr.co/edit/k806WE

same thing is applicable for angular constants too.

like image 128
khanmizan Avatar answered Nov 15 '22 15:11

khanmizan


Our module

angular.module("data", [])
  .constant('apiPrefix', '/api/data');

We can override constant fully, like value.

angular.module('myapp', ['data'])
  .constant('apiPrefix', '/api2/data');

also we can override fully in config

angular.module('myapp', ['data'])
    .config(function ($provide) {
        $provide.constant('apiPrefix', '/api2/data');
    });

also we can override fully or partially (if object) in run

angular.module('myapp', ['data'])
    .run(function (apiPrefix) {
        apiPrefix = '/api2/data';
    });

But if we want to override constant with object partially in config (not in run), we can do something like this

angular.module("module1", [])
    .constant('myConfig', {
        param1: 'value1' ,
        param2: 'value2'
    });

angular.module('myapp', ['data'])
    .config(function ($provide, myConfig) {
        $provide.constant(
            'myConfig', 
            angular.extend(myConfig, {param2: 'value2_1'});
        );
    });
like image 32
Alexander Anikeev Avatar answered Nov 15 '22 14:11

Alexander Anikeev