angularjs - Is it possible to access constants without injection? -
i using gulp-ng-config in project dynamically generate constants @ build time.
this working fine, got me , colleague thinking how constants accessed in angular app.
if want access constants in controllers defined on same module constants belong to, still have inject constants name controllers can access values?
looking @ various examples, i'm pretty sure way - wondered if there other way didn't require further dependency injected.
the constants injection part of best-practice-approach. it's syntactical overhead preventing doing bad stuff defining global variables , using them on place. of course still can go ahead , define variables on global window-scope , use them everywhere, cleary wouldn't suggest so.
so can do
window.foo = 123;
and somewhere else
alert (window.foo);
this show wanted value but, of course, bad practice.
you can define variables on angular $rootscope , use them anywhere. yet, bottom line nothing else defining them on window scope , shouldn't done well.
the last option (and best scenario) define 1 constant object containing more values. way have inject 1 constant object instead of many different ones.
say have 2 constants far $width = 1024 $height = 768
there nothing bad defining 1 injectable constant
$config = { width: 1024, height: 768 };
if want access $constant outside angular world can this:
angular.element('body').injector().get('$constant')
assuming declared ng-app on 'body' element or further outside (like 'html' element).
Comments
Post a Comment