La Cotizada Ramirez La Cotizada Ramirez - 1 year ago 121
AngularJS Question

document getelementbyid options selectedindex in angularjs

This works in perfect javascript

document.getElementById("mySelect").selectedIndex = "0"

<select class="selectpicker" id="mySelect">
<option>English &nbsp;</option>
<option>Español &nbsp;</option>

How do you do this same but in Angularjs


Answer Source

One way is to add the ng-model attribute to the select tag, and then set the value of the matching variable in the scope (corresponding to the scope set in the controller). See the example below - bear in mind that value attribute were added to the options. And like tymeJV mentioned, ngOptions would typically be used in an angular application to set the option nodes inside the select tag.

  //set the model value to 'en', so the select list will select the option with value=='en'
  $scope.lang = 'en';
<script src=""></script>
<div ng-app="app" ng-controller="BodyController">
<select class="selectpicker" id="mySelect" ng-model="lang">
    <option value="en">English &nbsp;</option>
    <option value="es">Español &nbsp;</option>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download