我现在还不算很晚,但是如果您使用的是Grunt,那么我会取得很大的成功
grunt-ng-constant。
ngconstant在我的配置部分
Gruntfile.js看起来像
ngconstant: { options: { name: 'config', wrap: '"use strict";nn{%= __ngModule %}', space: ' ' }, development: { options: { dest: '<%= yeoman.app %>/scripts/config.js' }, constants: { ENV: 'development' } }, production: { options: { dest: '<%= yeoman.dist %>/scripts/config.js' }, constants: { ENV: 'production' } }}使用的任务
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']);所以运行
grunt server会产生一个
config.js文件
app/scripts/,看起来像
"use strict";angular.module("config", []).constant("ENV", "development");最后,我声明对任何需要它的模块的依赖:
// the 'config' dependency is generated via gruntvar app = angular.module('myApp', [ 'config' ]);现在,我的常量可以在需要的地方进行依赖注入。例如,
app.controller('MyController', ['ENV', function( ENV ) { if( ENV === 'production' ) { ... }}]);


