2016-07-14 84 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

很酷。没问题。您可以点击检查来指定答案是否正确,以便其他人也可以知道修正;) –