Angularjs 在ui-select2中选择并可选择的项目

Angularjs 在ui-select2中选择并可选择的项目,angularjs,ui-select2,Angularjs,Ui Select2,通常,一个项目在被选中后会从可选项目列表中删除。 但是,当我刷新项目的基础列表时,(已经)选择的项目也可以选择 我怎样才能避免这种情况 我的观点是这样的: <div ng-controller="MyCtrl" ng-init=buildList()> <button ng-click="buildList()">Refresh list</button> <select multiple ui-select2 data-ng-mode

通常,一个项目在被选中后会从可选项目列表中删除。
但是,当我刷新项目的基础列表时,(已经)选择的项目也可以选择
我怎样才能避免这种情况

我的观点是这样的:

<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() {

        // 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' }
        ];
    };
};

这可能没有什么帮助,但我发现ui-select2现在已不推荐使用。ui-select演示的工作方式与您希望更改版本(从ui-select2到ui-select)的方式相同,这无疑是一个长期的解决方案,但是现在,我需要一个快速修复此问题的方法。谢谢!
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' }
        ];
    };
};