栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Angular.js中配置不同的环境?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Angular.js中配置不同的环境?

我现在还不算很晚,但是如果您使用的是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' ) {    ...  }}]);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418700.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号