2014-12-04 71 views
0

通常,選擇項目後,將從可選項目列表中刪除項目。
但是,當我刷新項目的基礎列表時,(已經)選擇的項目也可以選擇!
我該如何避免這種情況?在ui-select2中選擇和選擇的項目

我的看法是這樣的:

<div ng-controller="MyCtrl" ng-init=buildList()> 
    <button ng-click="buildList()">Refresh list</button> 

    <select multiple ui-select2 data-ng-model="selectedDaltons"> 
     <option data-ng-repeat="dalton in daltons" value="{{dalton.id}}"> 
      {{dalton.name}} 
     </option> 
    </select> 
</div> 

我的控制器:

function MyCtrl($scope) { 
    // Averell is preselected (id:4) 
    $scope.selectedDaltons = [4]; 

    // $scope.daltons is iterated in ng-repeat 
    // its initially called in ng-init 
    $scope.buildList = function() { 
     $scope.daltons = [ 
      { id: 1, name: 'Joe' }, 
      { id: 2, name: 'William' }, 
      { id: 3, name: 'Jack' }, 
      { id: 4, name: 'Averell' }, 
      { id: 5, name: 'Ma' } 
     ]; 
    }; 
}; 

Here it is as a jsfiddle.

+0

這可能不是幫助,但我看到的用戶界面,選擇2現在已經過時https://github.com/angular-ui/ui-select2。 ui-select演示以你想要的方式工作http://plnkr.co/edit/juqoNOt1z1Gb349XabQ2?p=preview – 2014-12-04 14:48:40

+0

改變版本(從ui-select2到ui-select)絕對是長期的解決方案,但現在,我需要快速解決此問題。謝謝! – Jobacca 2014-12-08 08:27:01

回答

0

其實我覺得我找到了一個解決方案(我自己的問題):

根據原始問題的控制器,在更換i之前更改陣列T:

function MyCtrl($scope) { 
    // Averell is preselected (id:4) 
    $scope.selectedDaltons = [4]; 

    // $scope.daltons is iterated in ng-repeat 
    // its initially called in ng-init 
    $scope.buildList = function() { 

     // touch array before renewal!! 
     if (angular.isArray($scope.daltons)) { 
      $scope.daltons.pop(); 
     } 

     $scope.daltons = [ 
      { id: 1, name: 'Joe' }, 
      { id: 2, name: 'William' }, 
      { id: 3, name: 'Jack' }, 
      { id: 4, name: 'Averell' }, 
      { id: 5, name: 'Ma' } 
     ]; 
    }; 
}; 

Here the updated jsfiddle.