Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear icon 'x' missing when using ng-template for ng-select

When using ng-template for the selected items. the 'x' icon to remove the element is missing, the package's documentation about using ng-template is kinda poor so I've been digging through their source with no luck.

I've put together this stackblitz so you can reproduce the issue

HTML

<form>
  <ng-select 
  [items]="availableItems" 
  [multiple]="true" 
  [closeOnSelect]="false"
    [hideSelected]="true" 
  [searchable]="true" 
  placeholder="Choose an item" 
  [(ngModel)]="selectedItems"
     name="item">
        <ng-template ng-label-tmp let-item="item">
            <div>{{item.name}}</div>
        </ng-template>
        <ng-template ng-option-tmp let-item="item">
            <div>{{item.name}}</div>
        </ng-template>
    </ng-select>
</form>

component.ts

export class AppComponent {

  availableItems = [
    { id: 1, name: 'name 1', value: 'value 1' },
    { id: 2, name: 'name2', value: 'value 2' },
    { id: 3, name: 'name 3', value: 'value 3' },
    { id: 4, name: 'name 4', value: 'value 4' }
  ];
  selectedItems = [
    { id: 2, name: 'name2', value: 'value 2' },
    { id: 3, name: 'name 3', value: 'value 3' },
  ]
}

Any thoughts?

like image 326
Danny22 Avatar asked Dec 13 '18 01:12

Danny22


1 Answers

When passing in a custom template, you need to define the clear button inside of it. Please refer to the documentation here: https://ng-select.github.io/ng-select#/multiselect

What you need to have is a template similar to the following:

<ng-template ng-label-tmp let-item="item" let-clear="clear">
    <span class="ng-value-label">{{item.login}}</span>
    <span class="ng-value-icon right" (click)="clear(item)">×</span>
</ng-template>

I've modified your StackBlitz to add this functionality. You can find it here.

like image 200
nipuna777 Avatar answered Sep 30 '22 08:09

nipuna777