Global variables

68 views
Skip to first unread message

Pushpendra Kumar

unread,
Feb 13, 2014, 8:41:22 AM2/13/14
to ang...@googlegroups.com
Hello All,
      
                 I want to have some global variables those will be initialized on login.
                 And then I will use these variables in whole application.
                 Please suggest e an idea to have global variables and constants.



Thanks,
Pushpendra 

Raul Vieira

unread,
Feb 13, 2014, 8:52:47 AM2/13/14
to ang...@googlegroups.com
To have variables visible throughout your ng application I would consider adding them to the root scope.  If these vars need to visible outside of ng then you'll need to add them to the window (hopefully in a namespace you've created).

Raul

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Bernhard Rode

unread,
Feb 13, 2014, 9:01:19 AM2/13/14
to ang...@googlegroups.com
I'd recommend to implement services to hold your shared data and deal with the communications etc. 

You may also bind those services to your rootScope in the applications run method.
--

Bernhard Rode
M.Sc. (Master of Computer Science and Media)

fon +49.171.5629409
mail ma...@bernhardrode.de
twitter @ebbo

google+ google.com/+BernhardRode
twitter twitter.com/ebbo
facebook facebook.com/bernhard.rode

Alesei N

unread,
Feb 13, 2014, 9:53:49 AM2/13/14
to ang...@googlegroups.com
You can use

.value('config',{});

Then inject it in your main controller, where you could expose this config through $rootScope.

Let me know if you need more configuration.

Jeff Hubbard

unread,
Feb 13, 2014, 5:24:03 PM2/13/14
to ang...@googlegroups.com
This is what I would recommend as well. Injection is awesome, and you should use as much of it as you need--rather than polluting scopes or having a "configuration holding" service.

Raul Vieira

unread,
Feb 13, 2014, 5:28:34 PM2/13/14
to ang...@googlegroups.com, ang...@googlegroups.com
My understanding was that the config block runs during app bootstrapping.  I read the requirement to indicate on login.  If login is part of the same app then how will this work?

Sent from my iPhone
--

Martin Alix

unread,
Feb 13, 2014, 6:48:27 PM2/13/14
to ang...@googlegroups.com
Angular does not have a concept of login, that is something you have to implement yourself...

.value('config',{}) will be set as soon as the application starts and available anywhere it is injected.
It just happens to be named config in this example, it could be named anything, for example .value('foo',{bar:'baz'})

Raul Vieira

unread,
Feb 13, 2014, 7:29:53 PM2/13/14
to ang...@googlegroups.com, ang...@googlegroups.com
Yes I get that login is not core. My point was regarding the requirement of a login interaction. What was described was setting globals in this interaction. This implies after app bootstrapping. How can you use config in this scenario?

I agree that using a value makes sense, but not in the use case described. Did I misunderstand the request?

Raul

Sent from my iPhone

Alesei N

unread,
Feb 13, 2014, 10:50:13 PM2/13/14
to ang...@googlegroups.com
You can write to so called "config" after you injected it.

Initial post asks how to set global variables, this is one way to do this, as this will be available through out app any where you inject it.

you could also approach this through.

app.run(function ($rootScope) {
    $rootScope.someData = {message: "hello"};
});

Pushpendra Kumar

unread,
Feb 14, 2014, 2:10:20 AM2/14/14
to ang...@googlegroups.com
Thanks a lot guys.

What about the use of app.costant("myVars", {"a":12, "b":2});
I want that once my first call goes to back end and fetch response, I want to keep this response in whole app.


Pushpendra 

Alesei N

unread,
Feb 14, 2014, 8:30:03 AM2/14/14
to ang...@googlegroups.com
That works too, if you are planning to only read it. You could use run block to execute some logic, and assign constant.

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/wMjutZlWq54/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages