最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to declare enums in angularjs? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jun 8, 2016 at 13:14 membersound asked Jun 8, 2016 at 13:10 membersoundmembersound 86.8k216 gold badges671 silver badges1.2k bronze badges 3
  • Ok then what are remended ways of declaring the enum inside? – membersound 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. – Pointy Commented Jun 8, 2016 at 13:13
  • Yes, probably I'm asking this... from the java point of view. – membersound Commented Jun 8, 2016 at 13:15
Add a ment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论