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?

billc.cn
  • 7,187
  • 3
  • 39
  • 79
  • Show an example of what you want to store. – Shomz Jun 05 '15 at 09:37
  • Similarly,you can use the constant in angularjs. http://stackoverflow.com/questions/18494050/is-there-a-way-in-angularjs-to-define-constants-with-other-constants – Srigar Jun 05 '15 at 09:55

2 Answers2

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.

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
  • 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. – billc.cn Jun 05 '15 at 09:55
  • Just implemented this. Worked very well. Thank you very much :) – billc.cn Jun 05 '15 at 10:27
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

Srigar
  • 1,648
  • 3
  • 14
  • 28