Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare enums in angularjs?

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.

like image 991
membersound Avatar asked Jun 08 '16 13:06

membersound


People also ask

How do you declare an enum?

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays. Above, the WeekDays enum declares members in each line separated by a comma.

Can we declare enum in JavaScript?

Enums or Enumerated types are special data types that set variables as a set of predefined constants. In other languages enumerated data types are provided to use in this application. Javascript does not have enum types directly in it, but we can implement similar types like enums through javascript.

What is enums in angular?

Enums are one of the features exclusive to TypeScript. We can use them to define a list of named constants, which lets us create easier-to-understand code by documenting distinct cases. TypeScript includes numeric and string-based enums. And we can also assign computed values to non-const enums.

Can I declare enum in class?

Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.


1 Answers

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

like image 161
murli2308 Avatar answered Nov 08 '22 17:11

murli2308