Using angular, How do I get selected value on ng-change on a dropdown?

Armeen Harwood picture Armeen Harwood · Jan 9, 2015 · Viewed 20.5k times · Source

So I have some data coming in a string seperated by commas.

sizes: "Small,Medium,Large,X Large,XX Large"

I got a drop down that displays value based on splitting that string.

<select ng-options="choice as choice for (idx, choice) in val.sizes.split(',')" 
        ng-change="selected.product.set.size(choice);>

        <option value="">Please select a size</option>
</select>

Using ng-change: How do I pass the selected value choiceto a function?

Answer

PSL picture PSL · Jan 9, 2015

You could use an ng-model and access it in your ng-change callback, or just pass it through.

<select ng-model="selectedSize" ng-options="choice as choice for (idx, choice) in val.sizes.split(',')" 
        ng-change="selected.product.set.size(selectedSize)">

        <option value="">Please select a size</option>
</select>

angular.module('app', []).controller('ctrl', function($scope) {
  $scope.sizes = "Small,Medium,Large,X Large,XX Large";
  $scope.handleChange = function() {
    console.log($scope.selectedSize)
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <select ng-model="selectedSize" ng-options="choice as choice for (idx, choice) in sizes.split(',')" ng-change="handleChange()">

    <option value="">Please select a size</option>
  </select>
</div>