Best way to override module values/constants in angularJS

前端 未结 3 2375
一个人的身影
一个人的身影 2021-02-20 07:03

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

3条回答
  •  礼貌的吻别
    2021-02-20 07:52

    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.

提交回复
热议问题