Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
How to initially set a value in ng-select?

I have ng-select with list of values. If i click on selected value and click clear , need to set first option .Please help me how to sort it out . https://stackblitz.com/edit/ng-select-wdcg5a?file=app/app.component.ts

<ng-select class="" [items]="assigneeStatus" name="status_filter_id"
           bindLabel="lookup_label" bindValue="com_lookup_id" placeholder="Assignee Status"
           [(ngModel)]="searchAssigneeObj.status_filter_id" [hideSelected]="true"
           #status_filter_id="ngModel">
    <ng-template ng-label-tmp let-item="item">
        <span>{{item.lookup_label ||  'All'}}</span>
    </ng-template>
    <ng-template ng-option-tmp let-item="item" let-search="searchTerm"
                 let-index="index">
        <span>{{item.lookup_label || ''}}</span>
    </ng-template>
</ng-select>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There is no basic way to achieve this natively in ng-select.

You can define callback of (clear) event and add your first item in the array to achieve that goal.

HTML:

<ng-select [items]="cities3"
           bindLabel="name"
           multiple = true
           placeholder="Select at least one city"
           [(ngModel)]="selectedCities"
           (clear)="clearSelect()">
  <ng-template ng-label-tmp let-item="item">
    <b>{{item.name}}</b> is cool
  </ng-template>
  <ng-template ng-option-tmp let-item="item" let-index="index">
    <b>{{item.name}}</b>
  </ng-template>
</ng-select>    

TS:

export class AppComponent {
  selectedCities = [];
  cities3 = [
    {
      id: 0,
      name: 'Vilnius',
    },
    {
      id: 2,
      name: 'Kaunas',
    },
    {
      id: 3,
      name: 'Pavilnys',
    },
  ];

  constructor() {}

  addCustomUser = (term) => ({ id: term, name: term });

  public clearSelect() {
    this.selectedCities = [];
    this.selectedCities.push({
      id: 0,
      name: 'Vilnius',
    });
  }
}

See the codes:

multiple: true -> stackblitz

multiple: false -> stackblitz

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can achieve this using clear method.

in your component add below code :

public selectedCity;  

constructor() {
        this.selectedCity = [this.cities3[0]];
      }

then in the ng-select add clear event as below :

<ng-select 
     [items]="cities3"
     bindLabel="name"
     multiple = true
     placeholder="Select city"
     (clear)="resetCalculations();"
     [(ngModel)]="selectedCity">
            <ng-template ng-label-tmp let-item="item">
                <b>{{item.name}}</b> is cool
            </ng-template>
            <ng-template ng-option-tmp let-item="item" let-index="index">
                <b>{{item.name}}</b>
            </ng-template>
  </ng-select>  

and then register the handler as below :

 resetCalculations() {
    this.selectedCity = [this.cities3[0]];
  }

here is the working example : Working Demo

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda