Select by default value for dropdown not working by using 'track by' in angualrjs

Issue

I am using track by to avoid strange values ::string that are appending to value but when I select by default dropdown, dropdown is not getting selected..
this is my plunker

Example Plunker

Solution

(function(angular) {
  'use strict';
angular.module('defaultValueSelect', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.data = {
     availableOptions: [
       {id: '1', name: 'Option A'},
       {id: '2', name: 'Option B'},
       {id: '3', name: 'Option C'}
     ],
     selectedOption: '2' //This sets the default value of the select in the ui
     };
 }]);
})(window.angular);
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-select-with-default-values-production</title>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
  <script src="script.js"></script>
</head>
<body ng-app="defaultValueSelect">
  <div ng-controller="ExampleController">
  <form name="myForm">
    <label for="mySelect">Make a choice:</label>
   
      
    <select ng-model="data.selectedOption"> 
       <option ng-repeat="option in data.availableOptions track by option.id" value="{{option.id}}">{{ option.name}}</option> 
    </select>
  </form>
  <hr>
  <tt>option = {{data.selectedOption}}</tt><br/>
</div>
</body>
</html>

Working snippet of your demo

Please change your <select> tag as follows

<select ng-model="data.selectedOption"> 
   <option ng-repeat="option in data.availableOptions track by option.id" value="{{option.id}}">{{ option.name}}</option> 
</select>

Answered By – J-Mean

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published