0

I'm looking for a pattern in order to have globals constant in my application. But not with a controller or a factory. (so not with app.constant() too)

I just want to set a variable but I didn't find something good.

I wanted to set this var in my rootScoop but without success. with something like

myApp.run(function($rootScoop){
$rootScoop.global = {};
});

When I use that code, an arror occurs for nothing (transtateFilterProvider). When I delete this code, the translateService works,

I MUST have access in all html view, I don't want to always use a controller (useless in this case), I just want to set a global variable in rootScoop.

Thank you.

6
  • 2
    Why don't you want to use a factory/service? That seems like the angular way of doing it. If you really don't want to use that, you could try writing you variables on the global object (window) using pure JS (no angular): window.constants = {} Commented Jun 6, 2017 at 14:51
  • I want to access in HTML view, is that possible with windows.constants ? Commented Jun 6, 2017 at 14:53
  • Not sure to be honest. Try it out either using window.constants or window["constants"]. If that doesn't work you could expose that on the template's controller (scope). Commented Jun 6, 2017 at 14:56
  • actually I can inject my var in rootScope but when I do it, my injector throw an exception for nothing ... If I delete first line, it's works fine. var app = angular.module("app", []); app.run(function($rootScope){ $rootScope.constants = { "client": { 'name' : 'client' } }; console.log($rootScope); }); Commented Jun 6, 2017 at 15:00
  • Not sure I understand what you are trying to say. ;) In your controller you could do following to expose the global constants object to the "angular world": $scope.constants = window.constants - but be aware that you would need to do this in all controllers where you want to access your constants. Commented Jun 6, 2017 at 15:04

1 Answer 1

1

You are getting an error because it is :

$rootScope

And not

$rootScoop

Anyway, correct way to do this is to add a constant module to your app like :

angular.module('yourapp', []).constant('Constants', {
    foo: 'bar'
});

But you'll have to call Constants in controllers.

If you use $rootScope, remember you will need to call $root.global in templates.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.