从单独的JS文件中检索常量\字符串值

时间:2021-02-18 13:31:49

I am developing an Angular JS application. I would like to know what is the best practice to include string values in our code.

我正在开发一个Angular JS应用程序。我想知道在代码中包含字符串值的最佳做法是什么。

I am planning to use another JS File which contains all the constant values. I will call this JS file in each and every JS(Controller) to refer the string values.

我打算使用另一个包含所有常量值的JS文件。我将在每个JS(Controller)中调用此JS文件来引用字符串值。

5 个解决方案

#1


0  

Here is the sample code;

这是示例代码;

var app = angular.module('plunker', []);

app.constant('configs', {host:'localhost',port:8080});
app.controller('MainCtrl', function($scope, configs) {
  $scope.name = configs.host;
});

here is demo plunker

这里是demo plunker

#2


4  

You can define constants in angular like

您可以像角度一样定义常量

angular.module('moduleName',[]).constant('constName',string|object|array);

You can inject in directive or controller or wherever you want.

您可以注入指令或控制器或任何您想要的地方。

angular.module('moduleName',[]).directive('directiveName', ['constName', function(constName){...});

#3


2  

You have several options.

你有几个选择。

Global value. You can use your constants in form of the javascript object which would be globally accessible across the application. For example, your file could look something like this:

全球价值。您可以使用javascript对象形式的常量,这些常量可以在整个应用程序中全局访问。例如,您的文件可能如下所示:

config = {
    host: 'domain',
    port: '1234' 
};

Obvious disadvantage is that those values are not really a constants and can be easily changed, so it's error prone.

明显的缺点是这些值实际上不是常量,很容易改变,因此容易出错。

Angular config module. More reliable and cleaner option is to create a separate module to be used as a dependency for main app module. You would still have separate file for your constants but instead of some global variable this file would hold angular module with constant service. Something like this:

Angular配置模块。更可靠和更清晰的选择是创建一个单独的模块,用作主应用程序模块的依赖项。对于常量,您仍然会有单独的文件,但是这个文件将保留带有常量服务的角度模块,而不是一些全局变量。像这样的东西:

angular.module('app.config', []).constant('config', {
    host: 'domain',
    port: '1234'
});

Then in main application you would configure app like

然后在主应用程序中,您将配置应用程序

angular.module('app', ['app.config']).config(function(config) {
    // console.log(config);
});

#4


0  

Angular has a built in way of providing constants, e.g:

Angular有一种内置的方式来提供常量,例如:

angular.module('foo', ['ngRoute']) // Your angularjs module

// Decide a name for the bucket of constants, then just declare the
// constants in turn as an object literal:
.constant("HTTP_CONSTANTS", {
    "URL": "http://localhost",
    "PORT": "80"
})

Then you can load it anywhere where you have access too you foo module using dependency injection, i.e.:

然后你可以将它加载到你有访问权限的任何地方你使用依赖注入你foo模块,即:

angular.module('foo')

.controller('bar', function (HTTP_CONSTANTS) {
    ... // Use HTTP_CONSTANTS.URL or HTTP_CONSTANTS.PORT, for example
})

Here's a good primer on using constants:

这是使用常量的一个很好的入门:

http://twofuckingdevelopers.com/2014/06/angularjs-best-practices-001-constants/

#5


0  

You can use factory for this purpose and inject this factory where ever you want these constants.

您可以将工厂用于此目的,并将此工厂注入您想要这些常量的位置。

 var app=angular.module('Myapp', []);

    app.factory('ConstantService', function(){
      var constant={temp:'c'}; 
      var getConstants=function(){
       return constant;
          };
     return{
        constants:getConstants;
      }
    });

    app.controller('MyController',['ConstantService' function (ConstantService){
    var constant= ConstantService.constants;
    }]);

#1


0  

Here is the sample code;

这是示例代码;

var app = angular.module('plunker', []);

app.constant('configs', {host:'localhost',port:8080});
app.controller('MainCtrl', function($scope, configs) {
  $scope.name = configs.host;
});

here is demo plunker

这里是demo plunker

#2


4  

You can define constants in angular like

您可以像角度一样定义常量

angular.module('moduleName',[]).constant('constName',string|object|array);

You can inject in directive or controller or wherever you want.

您可以注入指令或控制器或任何您想要的地方。

angular.module('moduleName',[]).directive('directiveName', ['constName', function(constName){...});

#3


2  

You have several options.

你有几个选择。

Global value. You can use your constants in form of the javascript object which would be globally accessible across the application. For example, your file could look something like this:

全球价值。您可以使用javascript对象形式的常量,这些常量可以在整个应用程序中全局访问。例如,您的文件可能如下所示:

config = {
    host: 'domain',
    port: '1234' 
};

Obvious disadvantage is that those values are not really a constants and can be easily changed, so it's error prone.

明显的缺点是这些值实际上不是常量,很容易改变,因此容易出错。

Angular config module. More reliable and cleaner option is to create a separate module to be used as a dependency for main app module. You would still have separate file for your constants but instead of some global variable this file would hold angular module with constant service. Something like this:

Angular配置模块。更可靠和更清晰的选择是创建一个单独的模块,用作主应用程序模块的依赖项。对于常量,您仍然会有单独的文件,但是这个文件将保留带有常量服务的角度模块,而不是一些全局变量。像这样的东西:

angular.module('app.config', []).constant('config', {
    host: 'domain',
    port: '1234'
});

Then in main application you would configure app like

然后在主应用程序中,您将配置应用程序

angular.module('app', ['app.config']).config(function(config) {
    // console.log(config);
});

#4


0  

Angular has a built in way of providing constants, e.g:

Angular有一种内置的方式来提供常量,例如:

angular.module('foo', ['ngRoute']) // Your angularjs module

// Decide a name for the bucket of constants, then just declare the
// constants in turn as an object literal:
.constant("HTTP_CONSTANTS", {
    "URL": "http://localhost",
    "PORT": "80"
})

Then you can load it anywhere where you have access too you foo module using dependency injection, i.e.:

然后你可以将它加载到你有访问权限的任何地方你使用依赖注入你foo模块,即:

angular.module('foo')

.controller('bar', function (HTTP_CONSTANTS) {
    ... // Use HTTP_CONSTANTS.URL or HTTP_CONSTANTS.PORT, for example
})

Here's a good primer on using constants:

这是使用常量的一个很好的入门:

http://twofuckingdevelopers.com/2014/06/angularjs-best-practices-001-constants/

#5


0  

You can use factory for this purpose and inject this factory where ever you want these constants.

您可以将工厂用于此目的,并将此工厂注入您想要这些常量的位置。

 var app=angular.module('Myapp', []);

    app.factory('ConstantService', function(){
      var constant={temp:'c'}; 
      var getConstants=function(){
       return constant;
          };
     return{
        constants:getConstants;
      }
    });

    app.controller('MyController',['ConstantService' function (ConstantService){
    var constant= ConstantService.constants;
    }]);