javascript - How do I configure different environments in Angular.js? -


how manage configuration variables/constants different environments?

this example:

my rest api reachable on localhost:7080/myapi/, friend works on same code under git version control has api deployed on tomcat on localhost:8099/hisapi/.

supposing have :

angular     .module('app', ['ngresource'])      .constant('api_end_point','<local_end_point>')      .factory('user', function($resource, api_end_point) {         return $resource(api_end_point + 'user');     }); 

how dynamically inject correct value of api endpoint, depending on environment?

in php kind of stuff config.username.xml file, merging basic configuration file (config.xml) local environment configuration file recognised name of user. don't know how manage kind of thing in javascript?

i'm little late thread, if you're using grunt i've had great success grunt-ng-constant.

the config section ngconstant in gruntfile.js looks like

ngconstant: {   options: {     name: 'config',     wrap: '"use strict";\n\n{%= __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 use ngconstant like

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 generate config.js file in app/scripts/ looks like

"use strict"; angular.module("config", []).constant("env", "development"); 

finally, declare dependency on whatever modules need it:

// 'config' dependency generated via grunt var app = angular.module('myapp', [ 'config' ]); 

now constants can dependency injected needed. e.g.,

app.controller('mycontroller', ['env', function( env ) {   if( env === 'production' ) {     ...   } }]); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -