0

I have some static config-like values that are used throughout the web app. Currently I am creating directives to put them into the $scope, but that seems awfully wasteful.

Is there a way for angular expressions to directly access some kind of "static" values defined either in some module or in the Javascript global scope?

2
  • Show an example of what you want to store. Commented Jun 5, 2015 at 9:37
  • Similarly,you can use the constant in angularjs. stackoverflow.com/questions/18494050/… Commented Jun 5, 2015 at 9:55

2 Answers 2

2

First, consider if globals/statics are the way to go...

If so, define your statics as values in angular:

angular.module('globals', [])
    .value('serverConfig', 'xyz')
    .value('foo', { x:4, y: 5 })
    // etc

You can use those globals from code with normal dependency injection; use a run function to place them in the $rootScope, so that they are also available in expressions:

angular.module(...)
    .run(['$rootScope', 'serverConfig', 'foo', function($rootScope, serverConfig, foo) {
        $rootScope.serverConfig = serverConfig;
        $rootScope.foo = foo;
        // etc
    }]);

If you only need them available in expressions, just use the run() function.

Sign up to request clarification or add additional context in comments.

2 Comments

Almost forgot about run blocks. This seems to be a nice way of bridging the two worlds. I think I'll create an object in the root scope as a "namespace" for static things.
Just implemented this. Worked very well. Thank you very much :)
0

You can use the constant in angularjs,it will be helpful.

module.constant(name, object);

or

module.value(name, object);

or

you can use localStorage in html5(It cannot store the object),for storing object use localStorageModule third party js

2 Comments

I know about constants but I don't see a way to access them from angular expressions?
Try to store in $scope of the current controller and access it. Or else go with the $rootscope

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.