Search code examples
javascriptangularjsdependency-injectionconstantsangularjs-service

How to inject service to angular constant


I would like to define a constant which use $locale service. Constants are objects, so I can't inject it as parameter as in case of controller. How can I use it?

angular.module('app').constant('SOME_CONSTANT', {
  'LOCALE': $locale.id.slice(0, 2)
})

Solution

  • You can manually grab $locale with the $injector. Observe the following...

    app.constant('SOME_CONSTANT', { 
        'LOCALE': angular.injector(['ng']).get('$locale').id.slice(0, 2) 
    });
    

    JSFiddle Example