Skip to content
AngularJS Environment Plugin
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Revert removal of console.log(v.domain) to angular.environment.js. Apr 28, 2017
.editorconfig gitignore updated and added .editorconfig Jul 21, 2015
.gitignore gitignore updated and added .editorconfig Jul 21, 2015
LICENSE Updated contributors Apr 30, 2017
gulpfile.js Added StripDebug module to GulpFile and also as dev dependency Apr 26, 2017
index.js initial starting file easier for webpack/angular builds supporting es… Oct 22, 2015
karma.conf.js Add end line to end of karma.conf.js file. Apr 27, 2017
package.json Updated contributors Apr 30, 2017

AngularJS Environment Plugin

An useful plugin that allows you to set up different variables such as API Url, Sockets, Paths, etc, based on the context of script execution, which means you can set up (for example) different API endpoints depending if you are working on development, stage or production.


You can install this package either with npm or with bower.


bower install angular-environment


npm install angular-environment

Then add environment as a dependency for your app:

angular.module('yourApp', ['environment']);


Sometimes, during the development of our applications, we need to use different variables depending on what context our application is running.

Let's say you're working on an application that handles an API, and you have a version of your API running locally on your computer or laptop for testing purposes. Besides, you have the final or in-production API running on a server. Certainly, the API endpoints are not the same in both environments, so, this plugin allows you to work with same variable for the same purpose but using different values depending on what context your application is running: development, stage or production.

Even better, you can execute code depending on the running context. In some cases you probably could need run pieces of code only for development environment and not in for production, or vice versa.

Said that, let's go to configure the plugin and learn how to use it.


  • Once installed, inject the envServiceProvider into your Angular App config area.
  • Organize the environments as you wish under domains and vars objects.
  • You can use wildcards (*) to describe your domains, i.e.: *
  • As optional, you can set defaults variables under defaults object within vars, to catch not-defined variables in the environments.
  • Finally, in the same config area, you will need to check in which context your application is running, by adding envServiceProvider.check() which will automatically set the appropriate environment based on given domains.

Here's a full example:

angular.module('yourApp', ['environment']).
	config(function(envServiceProvider) {
		// set the domains and variables for each environment
			domains: {
				development: ['localhost', ''],
				production: ['', '*', ''],
				test: ['', '', 'acme.*.com'],
				// anotherStage: ['domain1', 'domain2']
			vars: {
				development: {
					apiUrl: '//',
					staticUrl: '//',
					// antoherCustomVar: 'lorem',
					// antoherCustomVar: 'ipsum'
				test: {
					apiUrl: '//',
					staticUrl: '//',
					// antoherCustomVar: 'lorem',
					// antoherCustomVar: 'ipsum'
				production: {
					apiUrl: '//',
					staticUrl: '//',
					// antoherCustomVar: 'lorem',
					// antoherCustomVar: 'ipsum'
				// anotherStage: {
				// 	customVar: 'lorem',
				// 	customVar: 'ipsum'
				// },
				defaults: {
					apiUrl: '//',
					staticUrl: '//'

		// run the environment check, so the comprobation is made
		// before controllers and services are built


In order to read the configured environment variables alongside your Angular App, you need to inject envService into your controllers or services:

controller('SomeController', ['$scope', 'envService', function($scope, envService) {
	// ...


Returns a string with the current environment

var environment = envService.get(); // gets 'development'


Sets desired environment. This will overwrite the settled environment during the automatically check in Angular config process (see Configuration topic).

envService.set('production'); // will set 'production' as current environment


Returns true or false if the given environment matches with the current environment.

if ('production')) {
	// actually, the current environment is production
	// so, let's make some logic only for production environment
else {
	// we're not in production environment


Returns the desired environment variable. If no argument is passed, this method will return all variables associated to the current environment.

var apiUrl ='apiUrl'); // gets '//localhost/api'

var allVars =; // gets all variables configured under the current environment

If the desired variable passed as argument doesn't exists in the current environment, the plugin will check into defaults object.


  • Support for adding domains with wildcards or regex.
  • Unit testing.
  • Support for protocols.


To report bugs or request features, please visit the Issue Tracker.

Contributing to this plugin

Please feel free to contribute to the plugin with new issues, requests, unit tests, code fixes or new features. If you want to contribute with some code, create a feature branch and send your pull request.


Copyright 2015-2017, Juan Pablo Barrientos Lagos (juanpablob)

Licensed under The MIT License
Redistributions of files must retain the above copyright notice.

You can’t perform that action at this time.