Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise
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.

share|improve this question
up vote 0 down vote accepted

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);
});
share|improve this answer
    
How is your answer different then mine ? – Don 13 hours ago
    
Both are same. When I started answering there was no answer. So I answered it. :) – Hitesh Kumar 13 hours ago

You can use constant.

angular.module('starter.services', [''])
    .constant("global", {
        "test": "1",
    })

Now inject this global in all controllers and use it.

module.controller('Friends', function(global) {
console.log(global);
})
share|improve this answer
    
I should do global.test to get 1 correct? – Alicia Brandon 12 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.