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

20,590

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>

Share:
20,590
Armeen Moon
Author by

Armeen Moon

I am a generalist; slowly becoming a specialist in Web Development. I mix art, design, and technology, to create effective experiences that deliver value at scale. My professional goals are simple: surround myself with smart, energetic, creative people while working on solving problems that matter. Specialties: Functional and Object Oriented JavaScript ,Angular+, AngularJS, AWS, CSS/SCSS, Vector/DOM motion graphics, semantic HTML, NodeJS, Golang, and passionate about learning i18n/l10n, a11y, and modern web workflow.

Updated on July 09, 2022

Comments

  • Armeen Moon
    Armeen Moon almost 2 years

    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?