AngularJS支持多种环境 [英] AngularJS support multiple environments
问题描述
什么是支持多种环境(开发,分期,生产,CI)在AngularJS应用程序?常规方式
What is the conventional way to support multiple environments(develop, staging, production, ci) in an AngularJS application?
有关更具体的例子,怎么会一次过有关设置(可能需要额外的工具)在配置的服务器端点的地方,然后在读取服务器端点用于其他地方,比如像通话$ http.get(server_endpoint +'/ REST / API /路由
?
For a more concrete example, how would one go about provisioning(probably requires an additional tool) a server endpoint in a config somewhere and then read that server endpoint in for use elsewhere, such as calls like $http.get(server_endpoint+'/rest/api/route'
?
推荐答案
您可以定义一个角度不变,将其注入所需的服务/控制器和参考环境特定值
You can define an Angular constant, inject it into the required service/controller and refer to environment-specific values
angular.module('YourApp')
.constant('AppConstants', {
'SERVER_ENDPOINT': 'http://api.example.com',
'SHOW_DEBUG': true
});
您会使用像
$http.get(AppConstants.SERVER_ENDPOINT + '/rest/api/route')
如果您使用咕噜或咕嘟咕嘟,有一个很好的任务,允许您根据环境创建一个角度不变的文件。
If you use Grunt or Gulp, there's a nice task that allows you to create an Angular constant file based on the environment.
步兵例如:
ngconstant: {
options: {
name: 'config',
},
dev: {
options: {
dest: '<%= yeoman.app %>/scripts/config.js'
},
constants: {
ENV: {
FB_APP_ID: '123456'
}
}
},
prod: {
options: {
dest: '<%= yeoman.dist %>/scripts/config.js'
},
constants: {
ENV: {
FB_APP_ID: '98765'
}
}
}
}
这ngconstant创建应用程序调用的生产和开发版本中的任务:分别开发
:督促和 ngconstant
The tasks that create the production and development version of the app call ngconstant:prod
and ngconstant:dev
respectively
这篇关于AngularJS支持多种环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!