2015-05-10 43 views
6

點擊「選擇黃色」按鈕,我想添加黃色到選定的列表。黃色正在被選中,但下拉菜單仍顯示爲黃色。以同樣的方式,我想點擊「取消選擇黃色」按鈕來取消選擇黃色。我可以取消選擇黃色,但黃色不顯示在下拉列表中。請幫我解決這個問題。 HTML:角度ui選擇多選:下拉列表沒有得到更新從控制器選擇一些項目

<ui-select multiple ng-model="multipleDemo.colors" theme="select2" ng-disabled="disabled" style="width: 300px;"> 
    <ui-select-match placeholder="Select colors...">{{$item}}</ui-select-match> 
    <ui-select-choices repeat="color in availableColors | filter:$select.search"> 
     {{color}} 
    </ui-select-choices> 
    </ui-select> 
    <p>Selected: {{multipleDemo.colors}}</p> 

    <input type="button" value="select yellow color" ng-click="selectYellowColor()"/> 
    <input type="button" value="deselect yellow color" ng-click="deselectYellowColor()"/> 

JS:

$scope.availableColors = ['Red','Green','Blue','Yellow','Magenta','Maroon','Umbra','Turquoise']; 
    $scope.multipleDemo = {}; 
    $scope.multipleDemo.colors = ['Blue','Red']; 

    $scope.selectYellowColor = function(){ 
    if($scope.multipleDemo.colors.indexOf($scope.availableColors[3]) == -1){ 
     $scope.multipleDemo.colors.push($scope.availableColors[3]); 
    } 
    }; 

    $scope.deselectYellowColor = function(){ 
    if($scope.multipleDemo.colors.indexOf($scope.availableColors[3]) != -1){ 
     var index = $scope.multipleDemo.colors.indexOf($scope.availableColors[3]); 
     $scope.multipleDemo.colors.splice(index, 1); 
    } 
    }; 

這裏是plunker鏈路http://plnkr.co/edit/AHZj1zAdOXIt6gICBMuN?p=preview

回答

7

UPD:這是在ui-select成分的issue。您可以使用我的解決方案作爲部分解決方法,直到此問題尚未解決爲止

ui-select不會過濾項目。它只是在的repeat屬性中評估您的表達。如果你想從建議中排除已經使用的值,你可以自己做。

添加額外的過濾器進入repeat

<ui-select-choices repeat="color in availableColors | filter:omitSelectedColors | filter:$select.search"> 

,然後定義您的過濾功能:

$scope.omitSelectedColors = function(color) { 
    return $scope.multipleDemo.colors.indexOf(color) === -1; 
} 
+0

謝謝,它幫助我在一定程度上。在選擇黃色時,下拉列表正在更新,黃色未列出。但是取消選中黃色後,它不會再次添加回到下拉列表中。更新了pluncker鏈接:http://plnkr.co/edit/zMWzYbOmHzfyfe9tob52?p=preview – SaiGiridhar

+0

找到一段代碼,負責在ui-select源代碼中。我的解決方案是解決此問題的方法:https://github.com/angular-ui/ui-select/issues/918。我想沒有辦法完全解決它在圖書館沒有改變 –

+0

感謝您的幫助。希望這個問題能夠儘早得到解決。你可以建議任何其他多選插件看起來相似(如果有的話)?我無法得到任何。 – SaiGiridhar