2016-07-14 85 views
2

我想弄清楚如何擺脫出現在我的下拉菜單中的空白空間。有一個不應該在菜單中的「全部」選項。我擺脫了這種選擇(可能不是正確的舉措),有時當我刷新我可以看到菜單,並有一個白色空間和其他時間,它只是給我一個控制檯上的錯誤。我不知道爲什麼這個品種存在。如何擺脫下拉菜單選項/下拉菜單中的空白空間 - 角JS?

最終,我必須擺脫所有選項(正確的方式)並刪除它留下的空白區域。

這裏是我的JS:

define(["app", 
"lodash", 
"service/reference", 
"directives/control/topic-input" 
], function (app) { 
return app.directive('roleSelect', ['Reference', function (Reference) { 
    return { 
     restrict: 'A', 
     scope: { 
      selectedRole: '=', 
      selectedTopics: '=?', 
      presetRole: '=?', 
      topicNameOnly: '@?', 
      notUsePresets:'@?' 
     }, 
     link: function ($scope, elem, attr) { 
      $scope.talentRoles = []; 
      $scope.topicNameOnly = ($scope.topicNameOnly === 'true'); 
      $scope.notUsePresets = ($scope.notUsePresets === 'true'); 
      if(!$scope.presetRole) { 
       $scope.presetRole = {id: -1, code: "ALL", description: "All"}; 
      } // this is where the 'All' option comes from, so I just got rid of this if statement. Not sure if this is the right thing to do 
      if(!$scope.notUsePresets) { 
       $scope.talentRoles.push($scope.presetRole); 
      } 
      Reference.getTalentRoles($scope, function (response) { 
       $scope.talentRoles = response.concat($scope.talentRoles); 
       for(var i = 0;i<$scope.talentRoles.length;i++){ 
        if($scope.selectedRole && $scope.selectedRole.description == $scope.talentRoles[i].description){ 
         $scope.selectedRole = $scope.talentRoles[i]; 
        } 
       } 
      }); 
      $scope.showTopics = function(){ 
       return attr.selectedTopics 
      } 
      $scope.roleChanged = function(role) { 
       $scope.selectedTopics = []; 
      }; 
     }, 
     templateUrl: '/directives/control/role-select.html' 
    }; 
}]) 
}); 

這裏是我的html:

<select class="form-control" required 
    id="roleSelector" 
    ng-options="talentRole as talentRole.description for talentRole in talentRoles track by talentRole.id" id="talentRole" 
    ng-change="roleChanged(role)" 
    ng-model="selectedRole"> 
<option ng-if="true" selected="true" value="" class="select-placeholder"><ln code="directive.role.select.tooltip" args=""></ln></option> 
</select> 
<div topic-input 
name-only="topicNameOnly" 
class="row-spacing" 
ng-show="showTopics()" 
ng-model="selectedTopics" 
talent-role-id="selectedRole.id"></div> 

我不是太熟悉的角度JS/HTML。不過,我確實知道<option></option>標籤可以確保從用戶端選擇的任何內容都保持在我身邊(作爲管理員)。所以,這個功能需要保留。我也明白,ng-options是這個菜單的母親。所以也許需要使用這個標籤進行一些配置。

這裏是一個快照刪除從JavaScript中的if語句前:

With All

看來,當我刪除if語句,我得到的控制檯上的錯誤:

類型錯誤:無法讀取undefined的屬性「描述」

所以,我猜想擺脫這種情況並不是正確的做法。如果我沒有給予足夠的支持,我表示歉意,但請讓我知道我可以做些什麼來幫助您進一步幫助我,我將非常樂意接受。

回答

2

我想你應該從你的$ scope初始化中刪除presetRole: '=?',如果你刪除if

此外,您還需要檢查其他任何引用presetRole,因爲它似乎,即使你刪除if,該presetRole仍然加入talentRoles

告訴我,如果它的工作。

+0

工作。謝謝你 – keslami

+0

很酷。沒問題。您可以點擊檢查來指定答案是否正確,以便其他人也可以知道修正;) –