Ludmila Ludmila - 1 year ago 74
Javascript Question

Can't get access to Angular constant

I'm trying to get access to my angular

via this code:

.constant('MY_CONSTANTS', {
myConstant_1: 'My constant'

My controller:

.controller('MyController', ['$log', '$rootScope', '$location', MY_CONSTANTS, MyController]);

function MyController($log, $rootScope, $location, MY_CONSTANTS, MyController) {

But i can't get the value and output the value from my constants. What's wrong with my code?

Answer Source

You have to wrap dependency inside single quote/double quote while specifying it inside an array. Here you are missing quotes aroud MY_CONSTANTS.

And then at the end of DI Array, it should be your controller function ArticleController not MyController

   ['$log', '$rootScope', '$location', 'MY_CONSTANTS', ArticleController]);
                                      //^^^^^ change here..
function ArticleController($log, $rootScope, $location, MY_CONSTANTS) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download