所以我有一些数据以逗号分隔的字符串形式出现。
sizes: "Small,Medium,Large,X Large,XX Large"
我得到一个下拉列表,显示基于拆分该字符串的值。
<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>
使用 ng-change
:如何将选定的值 choice
传递给函数?
最佳答案
您可以使用 ng-model
并在您的 ng-change
回调中访问它,或者直接传递它。
<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>
关于javascript - 使用 Angular ,如何在下拉列表中的 ng-change 上获取选定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27852492/