How to add service to angular constant
2 answers
You can manually grab $locale
with $ injector . Observe the following ...
app.constant('SOME_CONSTANT', {
'LOCALE': angular.injector(['ng']).get('$locale').id.slice(0, 2)
});
+2
source to share
this is not possible for two reasons.
-
constant cannot have dependencies (see table below https://docs.angularjs.org/guide/providers )
-
constants and provider are available in .config functions (config phase), but services ($ locale) are only available later (in .run function / phase)
Alternatively, you can create a factory service type that can have dependencies and can create an object or primitive
angular.module('app')
.factory('LOCALE_ID', function($locale) {
return {'LOCALE': $locale.id.slice(0, 2)}
})
+5
source to share