imsi imsi imsi imsi - 5 days ago 6
AngularJS Question

How to get value from dropdown list with angularjs

I populate a select box with data from backend and it works well, but when I click on an item to get it value, it gives me undefined:

<div class="col-md-5">
<label class="child-label" for="existing-phases">Existing:&nbsp;&nbsp;</label>
<select class="form-control" ng-model="MyData">
<option disabled selected value>-- select an option --</option>
<option ng-repeat="var in payloadSecteur" value="{{var.id}}">{{ var.secteur }}
</option>
</select>
<a class="btn btn-primary add-contract-button pull-right" ng-click="showPopup()">+</a>
</div>




Here's the function:

$scope.showPopup=function(){
alert('eee');
alert($scope.MyData);
};

Answer

This is a working snippet of your code without the value attribute set in the option tag and the options have been hard coded ,it is working perfectly

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <label class="child-label" for="existing-phases">Existing:&nbsp;&nbsp;</label>
  <select class="form-control" ng-model="MyData">
    <option disabled selected value>-- select an option --</option>
    <option>heldfd</option>
    <option>asdf</option>
    <option>asdf</option>
    <option>asdf</option>
  </select>
  <a class="btn btn-primary add-contract-button pull-right" ng-click="showPopup()">+</a>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.showPopup=function(){
  alert($scope.MyData);
};
});
</script>

Comments