2017-08-08 73 views
-1

我们通过ng-options以一种形式进行多个下拉菜单,但是如果我们更改一个下拉菜单,则所选值也会与另一个下拉菜单匹配。通过ng-option不起作用的多个下拉菜单

<div ng-app="SelectApp"> 
    <div ng-controller="selectController"> 

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

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

</div> 

JS代码:

'use strict'; 
var app = angular.module('SelectApp', [ ]); 
app.controller('selectController', ['$scope', '$window', function ($scope, $window) { 
    $scope.categories = [  
     { id: 0, name: "Select a category..."}, 
     { id: 1, name: "Cars", group : "- Vehicles -" }, 
     { id: 2, name: "Commercial vehicles", disabled: false,group : "- Vehicles -" }, 
     { id: 3, name: "Motorcycles", disabled: false, group : "- Vehicles -" }, 
     { id: 4, name: "Car & Motorcycle Equipment", disabled: false, 
     group : "- Vehicles -" }, 
     { id: 5, name: "Boats", disabled: false, group : "- Vehicles -" }, 
     { id: 6, name: "Other Vehicles", disabled: false, group : "- Vehicles -" }, 
     { id: 7, name: "Appliances", disabled: false , group : "- House and Children -" }, 
     { id: 8, name: "Inside", disabled: false,group : "- House and Children -" }, 
     { id: 9, name: "Games and Clothing", disabled: false,group : "- House and Children -" }, 
     { id: 10, name: "Garden", disabled: false,group : "- House and Children -" } 
    ]; 

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

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

    }; 
}]);  
+0

您使用的是相同的NG-模型有两个下拉菜单,所以角度更新值到它的模型。 (ng-model =“itemSelected”) –

+0

对于每个选择框使用单独的模型 –

+0

感谢您的快速响应,但这些下拉列表来自某个后端系统的ng-repeat。我想要在不同的下拉菜单中设置不同的值, – ASHU

回答

1

我改变你的代码如下所示。希望这会起作用。

app.js

$scope.itemSelected = $scope.categories[0]; 
$scope.itemSelected1 = $scope.categories[0]; 
$scope.onCategoryChange = function (item,index) { 

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

}; 
}]); 

视图

<div ng-app="SelectApp"> 
<div ng-controller="selectController"> 

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

</select> 

<select name="category-group" id="categoryGroup" class="form-control" ng-model="itemSelected1"           ng-change="onCategoryChange(itemSelected1)" 
    ng-options="category.name group by category.group for category in categories"> 

</select> 

</div> 

+0

嗨srin,感谢您的帮助,但仍然我的问题尚未解决我更新我的提琴手,当我从第一个下拉列表中选择我正在xyz绑定的值正确显示,但是当我从第二个下拉列表中选择其更新第一个和第二两个具有相同的价值,我希望它的不同价值选择个别 http://jsfiddle.net/sensex00007/mLrynxh2/205/ – ASHU

+0

更新检查你的小提琴 –

+0

对不起,其不更新可以请你che它 – ASHU