我有一个下拉菜单,其中的选项来自使用 ng-repeat 的数组。它像正常的下拉菜单一样工作。但我想要以不同的方式。如果选择了一个选项,则应在其他下拉列表中禁用它。我们可以用 Angular 这样做吗?
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-repeat="address in addresses">
Billing State:
<select
ng-model="address.state"
ng-options="state.lookupCode as state.description for state in lov_state"></select>
<tt>State selected: {{address.state}}</tt>
</div>
</div>
Controller 代码:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope) {
$scope.addresses = [
{'state': 'AL'},
{'state': 'CA'},
{'state': 'FL'}
];
$scope.lov_state = [
{'lookupCode': 'AL', 'description': 'Alabama'},
{'lookupCode': 'FL', 'description': 'Florida'},
{'lookupCode': 'CA', 'description': 'California'},
{'lookupCode': 'DE', 'description': 'Delaware'}
];
});
请帮我做到这一点。这是 jsfiddle 链接。
最佳答案
您可以通过在 for< 之前的位置使用
关键字。disable when condition
来使用 ng-options
中提供的 disable
选项
<div ng-repeat="address in addresses">
Billing State:
<select ng-model="address.state" ng-change="checkSelected(address.state)"
ng-options="state.lookupCode as state.description disable when (address.state != state.lookupCode && state.selected) for state in lov_state">
</select>
<tt>State selected: {{address.state}}</tt>
</div>
关于javascript - 如何使用 Angular 限制下拉菜单仅选择一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35426757/