La Cotizada Ramirez La Cotizada Ramirez - 4 days ago 4
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>
</select>


How do you do this same but in Angularjs

app.controller("BodyController",function($scope){
/****************codeeeeeeeeeeeeeeeeeeeeeeeeee*/
});

Answer

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.

angular.module('app',[])
.controller("BodyController",function($scope){
  //set the model value to 'en', so the select list will select the option with value=='en'
  $scope.lang = 'en';
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></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>
</select>
                                          </div>