如何限制下拉列表使用angular只选择一个值?

时间:2016-02-16 07:53:45

标签: javascript angularjs ng-options angularjs-ng-options

我有下拉选项来自使用ng-repeat的数组。它正常工作。但我想以不同的方式。如果选择了一个选项,则应在其他下拉列表中禁用该选项。我们可以在棱角分明吗?

<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>

控制器代码:

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链接。

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

1 个答案:

答案 0 :(得分:2)

您可以在disable关键字之前的某个位置使用ng-options,使用disable when condition中提供的for选项。

<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