Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 946
Anubha Avatar asked Dec 11 '22 00:12

Anubha


2 Answers

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

like image 200
dting Avatar answered Dec 28 '22 23:12

dting


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

.select2-chosen{
   display: block !important;
}
like image 34
Hadnazzar Avatar answered Dec 29 '22 01:12

Hadnazzar