2014-02-25 68 views
1

我遇到了一個奇怪的問題,我似乎無法解決我的問題。我正在使用一個生成我網站樹形視圖的指令。當我選擇一個站點時,我有兩個選擇框,其中填充了一個站點中的組和站點中的列表。當選項填充時,它們全部被選中。我檢查了元素,他們都有option="selected"奇怪的是,當我點擊一個選項時,所有其他選項消失,只剩下選定的選項。我已經在Chrome控制檯中檢查了源代碼,只有選定的選項標記保留。ngOptions默認選擇了所有選項,並且選擇了消失選項

enter image description here

對於〔實施例的網站列出了選擇框有多種選擇,但是當我點擊了舊文件,但其他全部消失了。在網站用戶組,各組已經選定 Ctrl鍵:

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

回答

3

發生這種情況,因爲在選擇列表中的ngOptions是有界的同一個數組作爲ngModelngModel需要是僅保存選定值的不同數組。

隨着siteGroups爲例,正在發生的事情是,選擇列表選項與siteGroups初始化,他們都選擇了,因爲項目是ngModel(也siteGroups陣列)英寸當您點擊其中一個時,它現在會從ngModel中刪除您點擊的其他項目以外的所有其他項目。由於ngOptions限定在同一個列表中,所有未選定的選項也都消失。

要解決此問題,請在您的作用域上爲每個列表中的選定值創建單獨的數組屬性。

+0

是的,你是對的。我改變了ngModel,它完美的工作。 – Batman