0

constants.js file below

'use strict'

angular.module('app').constant('CONSTANT' { 
    "myValues": {
        "fruit": "apple",
        "car": "ford suv"
     }

I would like to simply access "myValues" from

ang-extention.js file and simply use it like:

function getMyStuff() {
    console.log(CONSTANT.myValues.fruit);
}

I tried:

angular.module(['CONSTANT', function(CONSTANT) {
    console.log(CONSTANT.myValues.fruit');
}]);

but no luck. How is it done in this case?

I searched various terms for example, "how to use constants in angularJS" I read bunch of answers but i didn't understand them so far and ended up asking SO myself.
Thank you so much in advance..

3
  • angular.module requires the name of the module to create/use... There is no function to provide. In angular.module('myModule').run(function(CONSTANT) { }); it will work. Commented Oct 22, 2019 at 22:14
  • Please ignore } missing in constants.js in the question. I forgot.. Commented Oct 22, 2019 at 22:14
  • @PierreEmmanuelLallemant Thanks, is ('myModule') -> can this be any name? Commented Oct 22, 2019 at 22:18

1 Answer 1

1

To use the constant, simply inject it into the .run function:

angular.module("app").run(function(CONSTANT) {
    console.log(CONSTANT.myValues.fruit);
});

For more information, see

Sign up to request clarification or add additional context in comments.

Comments

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.