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

时间:2014-12-04 14:34:04

标签: angularjs 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.

1 个答案:

答案 0 :(得分:0)

实际上我认为我找到了一个解决方案(对我自己的问题):

根据原始问题的控制器,在更换之前更改阵列:

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.