【问题标题】:ngOptions selected all options by default and disappearing options on selectngOptions 默认选择所有选项并在选择时消失选项
【发布时间】:2014-03-27 22:39:01
【问题描述】:

我遇到了一个奇怪的问题,我似乎无法用我的视图进行故障排除。我正在使用一个指令来生成我的网站的树形视图。当我选择一个站点时,我有两个选择框,其中填充了一个站点中的组和站点中的列表。 When the options populate, they are all selected.我检查了元素,它们都有option="selected" 奇怪的是,当我单击一个选项时,所有其他元素都消失了,只剩下选定的选项。我已经在 Chrome 控制台中检查了源代码,是的,只保留了选定的选项标签。

例如,站点列表选择框有多个选项,但是当我单击旧文档时,它们其他的都消失了。在站点组中,所有组都已被选中 控制:

spApp.controller('sitesCtrl',
    function sitesCtrl($scope, $q, $modal, UserService, GroupService, SiteService){
        //Options for tree controller directive
        $scope.treeOptions = {
            nodeChildren: "children",
            dirSelectable: true,
            injectClasses: {
                ul: "a1",
                li: "a2",
                liSelected: "a7",
                iExpanded: "a3",
                iCollapsed: "a4",
                iLeaf: "a5",
                label: "a6",
                labelSelected: "a8"
            }
        }

        //Returns siteMap for tree controller directive 
        $scope.siteMap = SiteService.getSiteMap();

        //Returns selected sites information: grous, lists, title, url
        $scope.showSelected = function(site){
            var siteData = SiteService.getSiteInfo(site);
            //sets sites title and url in view
            $scope.site = site;
            $scope.siteGroups = siteData.groups;
            $scope.siteLists = siteData.lists;
        }

    }
);

查看:

    <div class="siteGroups">
        <label for="siteGroups">Site Groups</label>
        <select 
            multiple
            name="siteGroups" 
            id="siteGroups" 
            class="siteGroups"
            ng-model="siteGroups"
            ng-options="g.name for g in siteGroups">
        </select>
    </div>
        <div class="btm1 animated fadeInUp">
        <label for="siteLists">Site Lists </label>
        <select multiple
            id="siteLists" 
            ng-model="siteLists"
            ng-options="l.title for l in siteLists">
        </select>
    </div>

Service and more of the view

【问题讨论】:

    标签: javascript angularjs select ng-options


    【解决方案1】:

    发生这种情况是因为选择列表中的ngOptionsngModel 绑定到同一个数组。 ngModel 需要是一个仅包含选定值的不同数组。

    siteGroups 为例,发生的情况是选择列表选项使用siteGroups 初始化,并且它们都被选中,因为这些项目在ngModel(也是siteGroups 数组)中。当您单击其中一个时,它现在会从 ngModel 中删除所有其他项目,但您单击的项目除外。由于ngOptions 绑定到同一个列表,所有未选中的选项也消失了。

    要解决此问题,请在您的范围内为每个列表中的选定值创建单独的数组属性。

    【讨论】:

    • 是的,你是对的。我更改了 ngModel,它运行良好。
    猜你喜欢
    • 1970-01-01
    • 2016-08-20
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2013-03-26
    • 2014-12-22
    相关资源
    最近更新 更多