3

所以我有一些数据以逗号分隔的字符串形式出现。

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传递给函数?

4

3 回答 3

9

您可以在回调中使用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>

于 2015-01-09T01:33:39.410 回答
0

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

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

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

于 2015-11-05T09:29:51.380 回答
-1

这肯定会有所帮助。

<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);
    }
});
于 2016-09-03T06:20:34.377 回答