5

I'd like to declare some enums that should be globally accessed from anywhere in my application, eg:

enum AIState { Asleep, Idling, Chasing, Fleeing, HavingLunch };

Question: where and how do I have to declare those enums withint an angularjs app?

main.js:

var myApp = angular.module('myApp', []);
myApp.config(...);

I later want to access them using AIState.Asleep, so I could pass them as a parameter and delegate my logic accordingly.

3
  • Ok then what are recommended ways of declaring the enum inside? Commented Jun 8, 2016 at 13:13
  • Are you asking for a way to create a set of enum-like constants? JavaScript has no native enum facility. Commented Jun 8, 2016 at 13:13
  • Yes, probably I'm asking this... from the java point of view. Commented Jun 8, 2016 at 13:15

1 Answer 1

12

use constant

angular
    .module('myApp', ['ngRoute'])
    .constant("myConfig", {
        "key": "value"
    })

you can inject constant as dependency and can use it

myApp.controller('myButton', ['myConfig', function(myConfig) {
  var k = myConfig['key'];
});

Basically you can use constant or value.

some references

  1. Constant

  2. Value

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

3 Comments

But how could I later access them via AIState.Asleep?
you can inject it as dependency and can use it in any controller, directive, services etc
this is not enum, this is key-value pair, and I do not want to duplicate key and value

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.