Use grunt-ng-constant .
Npm install this plugin:
npm install grunt-ng-constant
Then in grunt write to the configuration file:
ngconstant: { // Options for all targets options: { space: ' ', wrap: '"use strict";\n\n {%= __ngModule %}', name: 'config', }, // Environment targets development: { options: { dest: '<%= yeoman.app %>/scripts/config.js' }, constants: { ENV: { myvar1: 'Hello from devel', myname: 'John Doe' } } }, production: { options: { dest: '<%= yeoman.dist %>/scripts/config.js' }, constants: { ENV: { myvar1: 'Hello', myname: 'John Doe' } } } },
Then add the grunt task:
grunt.task.run([ 'clean:server', 'ngconstant:development', 'connect:livereload', 'watch' ]);
Running the task will generate config.js with the constants defined in the grunt file. Then add config.js to your index.html:
<script src="/scripts/config.js" />
Add it to your application:
var app = angular.module('myApp', [ 'config' ]);
And enter into the controller:
.controller('MainCtrl', function ($scope, $http, ENV) { console.log(ENV.myvar1); });
You can set different variables for production and different for development by installing it in the grunt file and installing ng: production or ng: development.
Refer to this article explaining the procedure for more information.