Alicia Brandon Alicia Brandon - 5 months ago 58
AngularJS Question

Using global variable (config.js) in angularjs module

angular.module('starter.services', [])
.controller('Friends', function() {
console.log(global);
})

angular.module('starter.anotherServices', [])
.factory('Friends', function() {
console.log(global);
})


Says I have few files for controller, factory and so on, how do I make a global variable that can be access by all of them? I have to create a .js file which will act as config like db credential and http request key.

Answer

You can create constant and inject where ever you want to access.

var app = angular.module('yourModule', []);
app.constant('Global', {
  baseUtl: 'www.google.com'
});

app.config(function(Global) {
  console.log(Global);
});
app.controller('YourController', function(Global) {
  console.log(Global);
});
Comments