9951 explained code solutions for 126 technologies


angularjsHow can I use an enum in an AngularJS application?


Enums in AngularJS applications are used to define a set of named constants. This can be useful when you want to define a set of values that will not change over time.

Here is an example of how to use an enum in an AngularJS application:

// Define an enum
var Color = {
  RED: 'red',
  GREEN: 'green',
  BLUE: 'blue'
};

// Use the enum in a controller
angular.module('myApp', [])
  .controller('MyController', function($scope) {
    $scope.selectedColor = Color.RED;
  });

The code above defines an enum called Color with three values (RED, GREEN, and BLUE). The enum is then used in a controller to set the initial value of a scope variable called selectedColor to RED.

Code explanation

  • var Color = { ... }: This is the definition of the enum. It is an object literal with three properties (RED, GREEN, and BLUE) that each have a string value.

  • angular.module('myApp', []): This is the definition of an AngularJS module.

  • .controller('MyController', function($scope) { ... }: This is the definition of a controller. It takes a $scope parameter, which is used to set the value of selectedColor to RED.

  • $scope.selectedColor = Color.RED;: This is the statement that uses the enum to set the value of selectedColor to RED.

Helpful links

Edit this code on GitHub