Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

javascript - 如何在Angular.js中配置不同的环境?(How do I configure different environments in Angular.js?)

How do you manage configuration variables/constants for different environments?(如何管理不同环境的配置变量/常量?)

This could be an example:(这可能是一个例子:)

My rest API is reachable on localhost:7080/myapi/ , but my friend that works on the same code under Git version control has the API deployed on his Tomcat on localhost:8099/hisapi/ .(我的rest API可以在localhost:7080/myapi/ ,但我的朋友在Git版本控制下使用相同的代码,他的Tomcat部署在localhost:8099/hisapi/ 。)

Supposing that we have something like this :(假设我们有这样的事情:)

angular
    .module('app', ['ngResource'])

    .constant('API_END_POINT','<local_end_point>')

    .factory('User', function($resource, API_END_POINT) {
        return $resource(API_END_POINT + 'user');
    });

How do I dynamically inject the correct value of the API endpoint, depending on the environment?(如何动态注入API端点的正确值,具体取决于环境?)

In PHP I usually do this kind of stuff with a config.username.xml file, merging the basic configuration file (config.xml) with the local environment configuration file recognised by the name of the user.(在PHP中,我通常使用config.username.xml文件执行config.username.xml操作,将基本??配置文件(config.xml)与用户名识别的本地环境配置文件合并。)

But I don't know how to manage this kind of thing in JavaScript?(但我不知道如何在JavaScript中管理这种事情?)   ask by rbarilani translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm a little late to the thread, but if you're using Grunt I've had great success with grunt-ng-constant .(我有点迟到了,但是如果你正在使用Grunt我已经用grunt-ng-constant取得了很大的成功。)

The config section for ngconstant in my Gruntfile.js looks like(对于配置部分ngconstant在我Gruntfile.js样子)

ngconstant: {
  options: {
    name: 'config',
    wrap: '"use strict";

{%= __ngModule %}',
    space: '  '
  },
  development: {
    options: {
      dest: '<%= yeoman.app %>/scripts/config.js'
    },
    constants: {
      ENV: 'development'
    }
  },
  production: {
    options: {
      dest: '<%= yeoman.dist %>/scripts/config.js'
    },
    constants: {
      ENV: 'production'
    }
  }
}

The tasks that use ngconstant look like(使用ngconstant的任务看起来像)

grunt.registerTask('server', function (target) {
  if (target === 'dist') {
    return grunt.task.run([
      'build',
      'open',
      'connect:dist:keepalive'
    ]);
  }

  grunt.task.run([
    'clean:server',
    'ngconstant:development',
    'concurrent:server',
    'connect:livereload',
    'open',
    'watch'
  ]);
});

grunt.registerTask('build', [
  'clean:dist',
  'ngconstant:production',
  'useminPrepare',
  'concurrent:dist',
  'concat',
  'copy',
  'cdnify',
  'ngmin',
  'cssmin',
  'uglify',
  'rev',
  'usemin'
]);

So running grunt server will generate a config.js file in app/scripts/ that looks like(所以运行grunt server会在app/scripts/中生成一个看起来像的config.js文件)

"use strict";
angular.module("config", []).constant("ENV", "development");

Finally, I declare the dependency on whatever modules need it:(最后,我声明了对任何模块的依赖:)

// the 'config' dependency is generated via grunt
var app = angular.module('myApp', [ 'config' ]);

Now my constants can be dependency injected where needed.(现在,我的常量可以在需要时进行依赖注入。)

Eg,(例如,)
app.controller('MyController', ['ENV', function( ENV ) {
  if( ENV === 'production' ) {
    ...
  }
}]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...