Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!