使用GruntJS的AngularJS开发模式

Jor*_*dan 6 javascript angularjs gruntjs yeoman

我有几个产品从yeoman角度发生器开始,它是一个非常好的开发设置.我无法找到一个好的解决方案是设置开发/生产模式标志.

当然,我们使用一些我们在生产中只需要的工具,因此使用prod/dev变量可以使用内联JavaScript和/或HTML文件非常有用.我以前在网上搜索过解决方案但没有找到任何有用的东西.

最终,我正在寻找一个在AngularJS设置中使用的好解决方案,理想情况下通过grunt服务和/或构建运行来设置.其他球队在这做什么?

gor*_*ate 4

我正在使用ng-constant。它创建一个 .js 文件,其中包含您选择的一些角度常数。

grunt.initConfig({
    ...
    ngconstant: {
        options: {
            name: 'config',
            dest: '<%= yeoman.app %>/scripts/config.js'
        },
        development: {
            constants: {
                myVariable: 'it is development'
            }
        },
        production: {
            constants: {
                myVariable: 'it is production'
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

然后将其添加到您的任务中:

grunt.registerTask('serve', [
    ...
    'ngconstant:development',
    ...
]);
Run Code Online (Sandbox Code Playgroud)

并且不要忘记将此 /scripts/config.js 文件包含在您的 html 中并将“config”注入您的应用程序。

var app = angular.module('myApp', [
    'config',
    ...
]);
Run Code Online (Sandbox Code Playgroud)