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.

Liam
  • 27,717
  • 28
  • 128
  • 190
Shining
  • 425
  • 6
  • 20
  • 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 = {}` – PzYon Jun 06 '17 at 14:51
  • I want to access in HTML view, is that possible with windows.constants ? – Shining Jun 06 '17 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). – PzYon Jun 06 '17 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); }); ` – Shining Jun 06 '17 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. – PzYon Jun 06 '17 at 15:04
  • Possible duplicate of [Global variables in AngularJS](https://stackoverflow.com/questions/11938380/global-variables-in-angularjs) – Liam Jun 06 '17 at 15:04

1 Answers1

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.

Bibzer
  • 264
  • 2
  • 4