首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ui-select2 2中可选择的项

在ui-select2 2中可选择的项
EN

Stack Overflow用户
提问于 2014-12-04 14:34:04
回答 1查看 598关注 0票数 0

通常,在被选中后,项目将从可选择项列表中删除。

但是,当我刷新项目的基本列表时,(已经)选定的项目也是可选择的!

我怎么才能避免这种情况?

我的看法如下:

代码语言:javascript
复制
<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>

我的控制器:

代码语言:javascript
复制
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' }
        ];
    };
};

在这里,它就像一只小提琴。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-08 08:37:13

实际上,我想我找到了一个解决办法(针对我自己的问题):

根据原始问题的控制器,在替换数组之前更改数组:

代码语言:javascript
复制
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' }
        ];
    };
};

这是最新的弹琴。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27296610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档