初始化ng-model时,Angular ui-select占位符不起作用

时间:2015-04-03 07:45:34

标签: javascript angularjs select angularjs-select2 ui-select

我每次点击按钮时都会尝试添加新的选择

html:

 <div ng-repeat = "select in selects track by $index">
  <ui-select ng-model="selects[$index]" theme="select2" ng-disabled="disabled" style="min-width: 300px;">
    <ui-select-match placeholder="Select a person in the list or search his name/age...">{{$select.selected.name}}</ui-select-match>
    <ui-select-choices repeat="person in people">
      <div ng-bind-html="person.name | highlight: $select.search"></div>
    </ui-select-choices>
  </ui-select>
 </div>
 <button ng-click="addNewSelect()"> Add new select</button>

控制器:

  $scope.selects = [{}];
  $scope.addNewSelect = function() {
    $scope.selects.push({});
  }

对象数组保存在数组'choices'中,但占位符不在选择中,因为我最初使用空对象初始化ng-model。如何让占位符在这种情况下工作?

以下是Plunker

3 个答案:

答案 0 :(得分:10)

.selected之后您遗漏了selects[$index]

如果没有这个,ui-select认为您选择了一个空对象(selects[$index])并且不会显示占位符。

<ui-select ng-model="selects[$index].selected" theme="select2" ng-disabled="disabled" style="min-width: 300px;">

http://plnkr.co/edit/56SHyE01BJ4EXglLlIcb?p=preview

您也不需要使用索引查找,只需使用select.selected

即可
<ui-select ng-model="select.selected" theme="select2" ng-disabled="disabled" style="min-width: 300px;">

http://plnkr.co/edit/3Uq8lDtDOaj5mIZVrCfv?p=preview

答案 1 :(得分:1)

添加css display的最快方法:阻止占位符项。

.select2-chosen{
   display: block !important;
}

答案 2 :(得分:0)

在某些情况下,上述解决方案效果很好,而在某些情况下效果不佳。

<ui-select ng-model="selectedChannel"
           on-select="$ctrl.channelSelected(selectedChannel)">
    <ui-select-match placeholder="Select Channel Type">
        {{selectedChannel.text}}
    </ui-select-match>
    <ui-select-choices ui-select-header-group-selectable="$ctrl.selectGroupHeader"
                       group-by="groupFindChannel"
                       repeat="channel in (r.channels | filter: $select.search) track by channel.id">
        <div ng-bind-html="channel.text | highlight: $select.search"></div>
    </ui-select-choices>
</ui-select>

^在这里让占位符显示出来...只需

$scope.selectedChannel = null;

希望有帮助...