javascript 在 angularJS 中覆盖模块值/常量的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22967895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:06:05  来源:igfitidea点击:

Best way to override module values/constants in angularJS

javascriptangularjs

提问by masimplo

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

我用 angularJS 编写了一个模块,它封装了所有后端通信。为了获得更大的灵活性,我将 api 前缀作为模块上的常量值(可能是值,因为我没有在配置阶段使用它)。所以像

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?

现在我想从两个不同的应用程序中使用这个模块。一个使用/api1/data,另一个使用/api2/data,我想在应用程序的配置阶段更改它。我知道如何使用提供者来做到这一点,但让提供者持有一个价值对我来说似乎是一种矫枉过正。我可以在应用程序配置阶段修改使用的模块常量或值吗?

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;
});

回答by khanmizan

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

在这里查看 plunker:http://plnkr.co/edit/k806WE

same thing is applicable for angular constants too.

同样的事情也适用于角度常数。

回答by Alexander Anikeev

Our module

我们的模块

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

We can override constantfully, like value.

我们可以constant完全覆盖,比如value.

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

also we can override fully in config

我们也可以完全覆盖 config

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

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

我们也可以完全或部分覆盖(如果对象) run

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

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

但是如果我们想constant在配置中部分覆盖对象(而不是在运行中),我们可以做这样的事情

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'});
        );
    });

回答by Robert Balicki

Angular modules, controllers, etc. can be contained within functions, if-statements, etc. They do not have to be at the top level. So, you could include this in your code:

Angular 模块、控制器等可以包含在函数、if 语句等中。它们不必位于顶层。因此,您可以将其包含在您的代码中:

if (environmentOne()) {
  module.value('apiPrefix','api1/data');
} else {
  module.value('apiPrefix','api2/data');
}

Hope that helps!

希望有帮助!