2016-02-16 36 views
0

我有下拉菜單,其中的選項來自使用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/

+0

這可以幫助你。 http://stackoverflow.com/questions/22005601/how-can-i-disable-to-select-the-particular-option-from-angularjs-dropdown。你可以使用類似的概念。 – chaoticmind

回答

2

你可以使用由之前for關鍵詞在一個地方使用disable when conditionng-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

+0

至點。你值得讚賞。 –

+0

@AlexRumba感謝人讚賞:) –

相關問題