最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular ui-select placeholder not working when ng-model is initialized - Stack Overflow

programmeradmin3浏览0评论

I am trying to add a new select every time a button is clicked,

The 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>

The controller:

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

The array of objects gets saved in the array 'selects', but the placeholder is not ing in the selects as I am initializing the ng-model with an empty object initially. How to get he placeholder working in this case?

Here is the Plunker for the same.

I am trying to add a new select every time a button is clicked,

The 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>

The controller:

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

The array of objects gets saved in the array 'selects', but the placeholder is not ing in the selects as I am initializing the ng-model with an empty object initially. How to get he placeholder working in this case?

Here is the Plunker for the same.

Share Improve this question edited Jul 31, 2017 at 2:00 Cœur 38.7k26 gold badges203 silver badges277 bronze badges asked Apr 3, 2015 at 7:45 AnubhaAnubha 1,4156 gold badges25 silver badges36 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 11

You are missing the .selected after the selects[$index].

Without this, the ui-select believes that you have selected an empty object (the selects[$index]) and won't show the placeholder.

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

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

also you dont need to look up using the index, you can just use select.selected

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

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

The fastest way to do that adding css display:block placeholder item.

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

There are instances where the above solution works great and where it does not.

<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>

^ Here to get the placeholder to show... just make

$scope.selectedChannel = null;

Hope it helps...

发布评论

评论列表(0)

  1. 暂无评论