2014-12-04 55 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.