Home:ALL Converter>Angular js select default dropdown menu value

Angular js select default dropdown menu value

Ask Time:2015-11-15T02:31:53         Author:Manish Basdeo

Json Formatter

Using angular js, I want the service2 value to be selected by default in the dropdown menu.

http://plnkr.co/edit/ssFHZEBuYWPUACEJ5GTb?p=preview

<!doctype html>
<html ng-app>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
    <script src="script.js"></script>

  </head>
  <body>

<div ng-controller="MyCntrl">
  <select ng-model="prop.value" ng-options="v.name for v in prop.values">
  </select>

  {{ prop.value.name}}
{{ prop.value.id}}
</div>


  </body>
</html>

script.js

 function MyCntrl($scope) {
      $scope.prop = {   "type": "select", 
        "name": "Service",
        "value":{"id":2, "name": "Service 2" }, 
    "values": [ {"id":1, "name": "Service 1" }, {"id":2, "name": "Service 2" }, {"id":3, "name": "Service 3" }, {"id":4, "name": "Service 4" }] 
      };

}

Here is what i want to achieve: 1) Change the selected object id and name 2) Select a value by default and display its id and name

In the example the id and name of the second service is well displayed. However, The dropdown menu fails to select the service 2.

How can I fix this ?

Regards

Author:Manish Basdeo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/33711854/angular-js-select-default-dropdown-menu-value
Adrian :

Are you really using angular 1.0? If you can use Angular >= 1.2 just use track by feature:\n\n<select ng-model=\"prop.value\" ng-options=\"v.name for v in prop.values track by v.id\">\n\n\nExample: Plunker\n\nResource:\nDocumentation for ngOptions",
2015-11-14T23:16:30
yy