abhishek kumar abhishek kumar - 5 months ago 23
Javascript Question

How to get selected values into scope from drop down while using nested ng-option?

Following is the view code I'm using to populate multiple select dropdown



<div ng-controller="sourceController">
<form novalidate ng-submit="submit()">
<div class="row">
<div class="addNewButton">
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal">Add New Data Source</button>
<div ng-include="'pages/modal.html'"></div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
Location:
<select id="location" ng-model="location" ng-options="city for (city, facilities) in sourceSelection " style="width: 100px">
<option value=''>Select</option>
</select>

</div>

<div class="col-sm-3">
Facility:
<select id="facility" ng-disabled="!location" ng-model="facility" ng-options="facility for (facility, phases) in location">
<option value=''>Select</option>
</select>

</div>

<div class="col-sm-3">
Phase :
<select id="phase" ng-disabled="!facility" ng-model="phase" ng-options="phase for phase in facility">
<option value=''>Select</option>
</select>

</div>

<div class="col-sm-3">
Device Type
<select id="deviceType" ng-disabled="!phase" ng-model="deviceType">
<option value=''>Select</option>
<option ng-repeat="deviceType in deviceTypes" value="{{deviceType}}">{{deviceType}}</option>
</select>
</div>
</div>

<input type="submit" id="submit" value="Submit" />

</form>
<button onclick="myFunction()">click here</button>
selected choices : {{location}} , {{facility}},

</div>





And the controller I'm using is



app.controller('sourceController', function($scope, $http, $rootScope) {
$scope.sourceSelection = {
"Chennai": {
"siruseri": ["phase1", "phase2"],
"chennai one": ["phase1"]
},
"kochi": {
"Kochi_technopark": ["phase1"]
}
};
});





Now I want to bind selected field (ie. selected location , Facility , phase in controller)with
$scope
in controller side and model I'm using there is giving me the value of the selected drop down key.

Answer

So following two lines are the answer , i got from my colleauge

$scope.getSelectedLocation= function(){
        var temp = document.getElementById("location");
        var location = temp.options[temp.selectedIndex].value;
      console.log(location);
} ;