Search code examples
javascripthtmlangularjsangularjs-ng-repeatangularjs-ng-options

Angular 1.3 - Add Ids to option tags inside ng-options


I am using Angular 1.3.8. I have a select list using an array as options with ngOptions and the syntax includes a basic track by expression.

I need to be able to add unique values to the ID attribute of the options (from that current color's description to the current <option> element when ng-options is creating the option tags. Is there any way to do that? If not, can I use ng-repeat? I have tried ng-repeat with no success.

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

  $scope.colors = [{
    "code": "GR",
    "description": "Green"
  }, {
    "code": "RE",
    "description": "Red"
  }, {
    "code": "CY",
    "description": "Cyan"
  }, {
    "code": "MG",
    "description": "Magenta"
  }, {
    "code": "BL",
    "description": "Blue"
  }];

  // Preselect CYAN as default value
  $scope.data = {
    colorType: $scope.colors[2]
  }

});
<script src="https://code.angularjs.org/1.3.8/angular.min.js"></script>
<div ng-app="plunker">
  <div ng-controller="MainCtrl">
    <strong>ng-options: </strong><select name="color" id="colors" ng-model="data.colorType" ng-options="color as color.description for color in colors track by color.code"></select>
<strong>ng-repeat: </strong><select name="color" id="colors" ng-model="data.colorType">
	<option ng-repeat="color in colors" value="{{color}}">{{color.description}}</option>
</select>
    <pre>{{ data | json }}</pre>
  </div>
</div>


Solution

  • One option is to use ngRepeat with the syntax (key, value) in expression

    (key, value) in expression where key and value can be any user defined identifiers, and expression is the scope expression giving the collection to enumerate.

    Using that syntax, the key (e.g. index) can be added:

    <option ng-repeat="(index, color) in colors" value="{{color}}" id="option_{{index}}">{{color.description}}</option>
    

    See it demonstrated below. Inspecting the options should reveal the id attributes set (e.g. option_0, option_1, etc).

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
    
      $scope.colors = [{
        "code": "GR",
        "description": "Green"
      }, {
        "code": "RE",
        "description": "Red"
      }, {
        "code": "CY",
        "description": "Cyan"
      }, {
        "code": "MG",
        "description": "Magenta"
      }, {
        "code": "BL",
        "description": "Blue"
      }];
    
      // Preselect CYAN as default value
      $scope.data = {
        colorType: $scope.colors[2]
      }
    
    });
    <script src="https://code.angularjs.org/1.3.8/angular.min.js"></script>
    <div ng-app="plunker">
      <div ng-controller="MainCtrl">        
    <strong>ng-repeat: </strong><select name="color" id="colors" ng-model="data.colorType">
    	<option ng-repeat="(index, color) in colors" value="{{color}}" id="option_{{index}}">{{color.description}}</option>
    </select>
        <pre>{{ data | json }}</pre>
      </div>
    </div>