简体   繁体   English

如何限制下拉列表使用角度仅选择一个值?

[英]How to restrict dropdown to choose only one value using angular?

I have dropdown in which options are coming from array using ng-repeat. 我有下拉列表,其中哪些选项来自使用ng-repeat的数组。 It is working as normal drop-down. 它像正常的下拉菜单一样工作。 But I want in a different way. 但是我想要一种不同的方式。 If one option is selected, it should be disabled in the other dropdowns. 如果选择了一个选项,则应在其他下拉菜单中将其禁用。 can we do like that in 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 code: 控制器代码:

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'}
    ];
});

Please help me to do that. 请帮我做到这一点。 Here is the jsfiddle link. 这是jsfiddle链接。

https://jsfiddle.net/santhosh1a32/3vd7hyrq/ https://jsfiddle.net/santhosh1a32/3vd7hyrq/

You could use disable option provided in ng-options it self by using disable when condition in a place before for keyword. 您可以通过在关键字for之前的disable when condition中使用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>

Demo Here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM