Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables in AngularJS

I have a problem where i'm initialising a variable on the scope in a controller. Then it gets changed in another controller when a user logs in. This variable is used to control things such as the navigation bar and restricts access to parts of the site depending on the type of user, so its important that it holds its value. The problem with it is that the controller that initialises it, gets called again by angular some how and then resets the variable back to its initial value.

I assume this is not the correct way of declaring and initialising global variables, well its not really global, so my question is what is the correct way and is there any good examples around that work with the current version of angular?

like image 477
Lightbulb1 Avatar asked Aug 13 '12 16:08

Lightbulb1


People also ask

What is global variable in angular?

We always need to declare global variable file for our angular 8 application because we can set some global variable there like site title, api url etc so you can easily access any where in our application easily. So, in this example, we will create GlobalConstants file for adding global variables in our application.

What are global variables with examples?

Example of Global Variable in C You can notice that in line 4, x and y get declared as two of the global variables of the type int. Here, the variable x will get initialized automatically to 0. Then one can use variables like x and y inside any of the given functions.

Which are global variables?

In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state.

What is the use of global variable?

Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside.


2 Answers

You've got basically 2 options for "global" variables:

  • use a $rootScope http://docs.angularjs.org/api/ng.$rootScope
  • use a service http://docs.angularjs.org/guide/services

$rootScope is a parent of all scopes so values exposed there will be visible in all templates and controllers. Using the $rootScope is very easy as you can simply inject it into any controller and change values in this scope. It might be convenient but has all the problems of global variables.

Services are singletons that you can inject to any controller and expose their values in a controller's scope. Services, being singletons are still 'global' but you've got far better control over where those are used and exposed.

Using services is a bit more complex, but not that much, here is an example:

var myApp = angular.module('myApp',[]); myApp.factory('UserService', function() {   return {       name : 'anonymous'   }; }); 

and then in a controller:

function MyCtrl($scope, UserService) {     $scope.name = UserService.name; } 

Here is the working jsFiddle: http://jsfiddle.net/pkozlowski_opensource/BRWPM/2/

like image 182
5 revs, 5 users 86% Avatar answered Sep 19 '22 12:09

5 revs, 5 users 86%


If you just want to store a value, according to the Angular documentation on Providers, you should use the Value recipe:

var myApp = angular.module('myApp', []); myApp.value('clientId', 'a12345654321x'); 

Then use it in a controller like this:

myApp.controller('DemoController', ['clientId', function DemoController(clientId) {     this.clientId = clientId; }]); 

The same thing can be achieved using a Provider, Factory, or Service since they are "just syntactic sugar on top of a provider recipe" but using Value will achieve what you want with minimal syntax.

The other option is to use $rootScope, but it's not really an option because you shouldn't use it for the same reasons you shouldn't use global variables in other languages. It's advised to be used sparingly.

Since all scopes inherit from $rootScope, if you have a variable $rootScope.data and someone forgets that data is already defined and creates $scope.data in a local scope you will run into problems.


If you want to modify this value and have it persist across all your controllers, use an object and modify the properties keeping in mind Javascript is pass by "copy of a reference":

myApp.value('clientId', { value: 'a12345654321x' }); myApp.controller('DemoController', ['clientId', function DemoController(clientId) {     this.clientId = clientId;     this.change = function(value) {         clientId.value = 'something else';     } }]; 

JSFiddle example

like image 40
Dean Or Avatar answered Sep 20 '22 12:09

Dean Or