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