Angularjs 下拉 OnChange 选定的文本和值

Angularjs Dropdown OnChange Selected Text and Value

我是 AngularJS 的新手,正在尝试从 Dropdown 中获取选定的文本和值。我遵循了很多教程,但仍然无法到达那里。 SelectedValueSelectedText 总是 undefined。下面是我的代码:

Html:

<div ng-app="SelectApp">
<div ng-controller="selectController">
    <select name="category-group" id="categoryGroup" class="form-control" ng-model="itemSelected" ng-change="onCategoryChange(itemSelected)">
        <option value="0">Select a category...</option>
        <option ng-repeat="category in categories" value="{{category.id}}"
             ng-disabled="category.disabled" ng-class="{'mainCategory' : category.disabled}">
             {{category.name}}
        </option>
     </select>
</div>

Js:

'use strict';
var app = angular.module('SelectApp', [ ]);
app.controller('selectController', ['$scope', '$window', function ($scope, $window) {

$scope.categories = [       
   { id: 1, name: "- Vehicles -", disabled: true },
   { id: 2, name: "Cars" },
   { id: 3, name: "Commercial vehicles", disabled: false },
   { id: 4, name: "Motorcycles", disabled: false },
   { id: 5, name: "Car & Motorcycle Equipment", disabled: false },
   { id: 6, name: "Boats", disabled: false },
   { id: 7, name: "Other Vehicles", disabled: false },
   { id: 8, name: "- House and Children -", disabled: true },
   { id: 9, name: "Appliances", disabled: false },
   { id: 10, name: "Inside", disabled: false },
   { id: 11, name: "Games and Clothing", disabled: false },
   { id: 12, name: "Garden", disabled: false }
];

$scope.onCategoryChange = function () {

    $window.alert("Selected Value: " + $scope.itemSelected.id + "\nSelected Text: " + $scope.itemSelected.name);

};
}]);

还有一件事,我将我的第一项定义为 Select a category... 然后为什么下拉列表中的第一项总是空的。

下面是我的 fiddle 样本。 http://jsfiddle.net/Qgmz7/136/

您应该使用 ng-options 将对象设置为您的 ng-model 值以更改您的 select 选项。

标记

<select name="category-group" id="categoryGroup" class="form-control" 
  ng-model="itemSelected" ng-change="onCategoryChange(itemSelected)" 
  ng-options="category.name for category in categories">
    <option value="0">Select a category...</option>
</select>

Fiddle Here

更新

为了保持样式,您必须在那里使用 ng-repeat,在这种情况下,您只会将 id 绑定到您的 ng-model 并且在检索整个对象时您需要过滤数据.

$scope.onCategoryChange = function () {
    var currentSelected = $filter('filter')($scope.categories, {id: $scope.itemSelected})[0]
    $window.alert("Selected Value: " + currentSelected.id + "\nSelected Text: " + currentSelected.name);
};

Updated Fiddle

那是因为,您的模型 itemSelected 捕获了 select 下拉列表的当前值,它只不过是 option 元素的 value 属性。你有

<option ng-repeat="category in categories" value="{{category.id}}">

在你的代码中,所以在呈现的版本中,你会得到

<option ng-repeat="category in categories" value="0">

但您希望 itemSelected 成为您的类别对象,任何查询 id 或其他 属性 的尝试都会 return undefined

您可以将 ng-optionsgroup by 一起使用,对您的数据稍做更改,或者您可以使用正常的 ng-repeat,获取 selectedIndex 并查找类别使用该索引从您的类别列表中删除对象。在这里展示第一种方法。

HTML

<select name="category-group" id="categoryGroup" 
        ng-model="itemSelected" ng-change="onCategoryChange(itemSelected)" 
        ng-options="category.name group by category.group for category in categories">
</select>

更新数据

$scope.categories = [
       { id: 0, name: "Select a category..."},
       { id: 1, name: "Cars", group : "- Vehicles -" },
       { id: 2, name: "Commercial vehicles", group : "- Vehicles -" },
       { id: 3, name: "Motorcycles", group : "- Vehicles -" }
 ];

 $scope.itemSelected = $scope.categories[0];

您可以添加一个 group 属性 而不是禁用 属性,它可以在 group by.

中使用

这是更新后的 Fiddle 来说明这个想法。

ngChange 仅 returns 您选择的选项的值,这就是为什么您没有获得全部数据的原因。

这是一个无需更改标记逻辑的有效解决方案。

标记:

<select
    name="category-group"
    id="categoryGroup"
    class="form-control"
    ng-model="id"     
    ng-change="onCategoryChange(id)">

ng更改处理程序:

 $scope.onCategoryChange = function (id) {
        //get selected item data from categories
        var selectedIndex = $scope.categories.map(function(obj) { return obj.id; }).indexOf( parseInt(id) );
        var itemSelected = $scope.categories[selectedIndex];

        $window.alert("Selected Value: " + itemSelected.id + "\nSelected Text: " + itemSelected.name);

    };

另一种解决方案(有点脏)是仅将选项的 value 更改为如下内容:

<option .... value="{{category.id}}|{{category.name}}">

...在您实际的 ngChange 处理程序中,只需拆分值即可将所有值作为数组获取:

$scope.onCategoryChange = function (itemSelected) {
    $scope.itemSelected = itemSelected.split('|'); //string value to array
    $window.alert("Selected Value: " + $scope.itemSelected[0] + "\nSelected Text: " + $scope.itemSelected[1]);

};
<div ng-app="SelectApp">
    <div ng-controller="selectController">
    <select ng-change='onCategoryChange()' ng-model="itemSelected" ng-options="category.name for category in categories">
        <option value="">-- category --</option>
    </select>
</div>

//http://jsbin.com/zajipe/edit?html,js,output

对您的 onCategoryChange() 稍作改动应该会奏效:

$scope.onCategoryChange = function () {
        $window.alert("Selected Value: " + $scope.categories[$scope.itemSelected - 1].id + "\nSelected Text: " + $scope.categories[$scope.itemSelected -1].name);

    };

JSFiddle:http://jsfiddle.net/Qgmz7/144/

这里是非常简单易行的代码我做了什么

<div ng-app="myApp" ng-controller="myCtrl">
Select Person: 
<select ng-model="selectedData">
   <option ng-repeat="person in persons" value={{person.age}}>
         {{person.name}}
   </option>
</select>
<div ng-bind="selectedData">AGE:</DIV>
<br>
</div>


    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl',myCtrlFn);
    function myCtrlFn($scope) {

    $scope.persons =[
        {'name': 'Prabu','age': 20},
        {'name': 'Ram','age': 24},
        {'name': 'S','age': 14},
        {'name': 'P','age': 15}
        ];
    }
    </script>