1

I'm trying to add constants to my angularjs application, but i'm getting undefined errors.

var appConstants = {

};

var appErrorMsgs = {
    loginFail: "Login failed, please check your credentials"
};



var URI_PATH = {
    registration: "/registration",
    seasons: "/seasons",
    vip: "vip",
};

angular.module('app.constants', []).
    constant('CONST', appConstants).
    constant('ERROR_MSG', appErrorMsgs).
    constant('URI_PATH', URI_PATH);

var app = angular.module('AppName', ['snap', 'ngRoute','ngResource','ngTouch','angular-carousel','FSControllers', 'FSPartials','FSDirectives', 'kinvey', 'app.constants']);
1
  • where are you getting the undefined error at, what line? Commented Jan 26, 2014 at 16:41

2 Answers 2

6

I guess you just forgot to inject them:

here is a plunker: http://plnkr.co/edit/4gav6PvJtPwJExchya2i?p=preview

app.controller('MainCtrl', function($scope, URI_PATH, ERROR_MSG, CONST) {
  console.log(CONST);
  console.log(ERROR_MSG);
  console.log(URI_PATH);
});
Sign up to request clarification or add additional context in comments.

2 Comments

That's right - constants can be defined but you still must inject them like anything else. Also, sometimes you may want "value" instead of "constant." The main difference is that value can be overridden but cannot be used in config blocks, constants can be used in config blocks but cannot be overridden.
@JeremyLikness to be accurate constants cannot be overridden only by an Angular decorator. It might be surprising a little but they can definitely be overridden anywhere else.
-1

I guess that the recommended solution would be to use a Service:

angular.module('AppName').factory('app.constants', [buildConstants]);
function buildConstants(){
    var appConstants = { };

    var appErrorMsgs = {
            loginFail: "Login failed, please check your credentials"
        };

    var URI_PATH = {
        registration: "/registration",
        seasons: "/seasons",
        vip: "vip",
    };

    return {
        CONST: appConstants,
        ERROR_MSG: appErrorMsgs,
        URI_PATH: URI_PATH
    };
}

And then you can inject that into your controllers and directives as you need them. Hope this helps.

4 Comments

I don't see any reason why to favor a service over a constant/value in this case. They are all types of providers.
Hi there, by using like this you don't need to inject every single constant like you did on your example, just need to inject the service and it will have all the constants... if there is something similar that's better, please share with us. Thanks
You can do it with a constant too, plnkr.co/edit/Ql5biQs4xJlLY619eVPt?p=preview. I didn't say you cannot use a service but why say it's better. I would also prefer a service/factory for almost anything but the question was about constants:)
Sure, I guess I´ve got a bit carried away, I just don´t think it´s practical to inject every single constant though... imagine that in the future you need to add a new one, you would have to modify all the controllers and directives to inject it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.