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.
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.
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.
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.
Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.
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
Constant
Value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With