Javascript 使用 angular,如何在下拉列表中获得 ng-change 的选定值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27852492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:46:41  来源:igfitidea点击:

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

javascriptangularjsdrop-down-menuangularjs-directiveangularjs-ng-change

提问by Matthew Harwood

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?

使用ng-change:如何将选定的值choice传递给函数?

回答by PSL

You could use an ng-modeland access it in your ng-changecallback, or just pass it through.

您可以ng-modelng-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>

回答by Silvan Hofer

For me using ng-model with just a simple variable does not work! So $scope.selectedSize would return undefined for me and $scope.selectedSize = "what size ever" wouldn't change the select drop down's model.

对我来说,只使用一个简单变量的 ng-model 是行不通的!所以 $scope.selectedSize 会为我返回 undefined 并且 $scope.selectedSize = "what size ever" 不会改变选择下拉的模型。

Is this even possible to work this way (byValue / byRef variable)?

这甚至可能以这种方式工作(byValue / byRef 变量)?

Using ng.model="whatever.selectedSize" and $scope.whatever.selectedSize works in my case for getting and setting the drop down's / the model's value.

在我的情况下,使用 ng.model="whatever.selectedSize" 和 $scope.whatever.selectedSize 可以获取和设置下拉列表/模型的值。

回答by Kartik Shah

This will surely help.

这肯定会有所帮助。

<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>

<div ng-controller="manageroles_controller" ng-app="app_manageroles">

    <select title="Update Role Hierarchy" name="cmb_role_hierarchy" id="cmb_role_hierarchy" ng-model="cmb_role_hierarchy" ng-change="updateRoleHierarchy('cmb_role_hierarchy');">

        <option ng-init="cmb_role_hierarchy=5" value="5">5</option>

    </select>

</div>

var app_manageroles =  angular.module('app_manageroles',[]);
app_manageroles.controller('manageroles_controller', function($scope,$http) {   
    $scope.updateRoleHierarchy = function(cntRoleHierarchy) {
        alert(cntRoleHierarchy);
    }
});