在AngularJS配置文件 [英] Configuration file in AngularJS

查看:526
本文介绍了在AngularJS配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是创建配置文件(有什么样的.NET Web配置),用于存储的URL可能在应用过程中部署变化的最佳方式,以及其他常量?

What is the best way to create config file (Something like web config in .net), for storing urls, and other constants that may vary during the application deploy?

推荐答案

使用 .constant()方法:

angular.module('app').constant('MONGOLAB_CONFIG', {
  baseUrl: '/databases/',
  dbName: 'ascrum'
});

这个例子

然后,在你需要的常量你可以注入它。

Then you can just inject it where you need the constants.

您可以定义开发或生产不同常数不同的文件,然后使用工具像咕噜根据环境用这样或那样的文件。

You can have different files defining different constants for development or production, and then use a tool like Grunt to use this or that file according to the environment.

让我们假设你有这样的文件夹结构:

Let's assume you have this kind of folder structure:

|-js/
|  |-app.js
|  |-anotherfile.js
|  |-...
|
|-env/
|  |-dev.js
|  |-prod.js
|
|-index.html

dev.js prod.js 定义相同 .constant()使用不同的值的服务。
然后你就可以得到适当的文件,用这样的gruntFile并置:

dev.js and prod.js defines the same .constant() service with different values. Then you can get the proper file to be concatenated with a gruntFile like that:

grunt.registerTask('default', ['concat']);

grunt.initConfig({
  env : process.env.NODE_ENV,
  src: {
    javascript: ['js/*.js'],
    config: ['env/<%= env %>.js']
  },
  concat: {
    javascript: {
      src:['<%= src.config %>', '<%= src.javascript %>'],
      dest:'myapp.js'
    }
  }
});

通过运行咕噜你会得到包含良好的常数a myapp.js 文件。

By running grunt you would get a myapp.js file containing the good constants.

修改:用咕嘟咕嘟你可以做这样的:

Edit: with Gulp you can do it like this:

var paths = [
  'env/' + process.env.NODE_ENV + '.js',
  'js/**/*.js',
];

var stream = gulp.src(paths)
  .pipe(plugins.concat('main.js'))
  .pipe(gulp.dest('/output'));

这篇关于在AngularJS配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆