ng-select defaultValue not working

Created on 15 Jun 2018  路  4Comments  路  Source: ng-select/ng-select

Hi I am new to angular. I tried to use ngx-select to do the search for dropdown. i set the defaultValue as {'id':'1', 'text': 'project 1'}. But the field shows the placeholder text instead of default value. Please advise how can I display the default value in the input dropdown field. thanks

projectIdList=[{'id':'1', 'text': 'project 1'}, 'id':'2', 'text': 'project 2'}, 'id':'3', 'text': 'project 3'}];

[(ngModel)]="selectedProjectId" name="projectId"
[defaultValue]="defaultProjectIds"
(selectionChanges)="projectIdSelectionChange($event)"
[multiple]="false" autoClearSearch="true"
[allowClear]="true">

More Info

ngx-select version: 3.5.9

Most helpful comment

wrong repo dude.

All 4 comments

wrong repo dude.

I use two way building like:
html:
[items] = "categoryItems"
class = "category-items"
name = "category"
bindLabel = "title"
bindValue = "id"
[(ngModel)] = "categoryDefaultValue"
dropdownPosition = "top"
(change)="slectCategory2($event)">

tc:
public categoryDefaultValue = 1; //set the default value(id)

Hi @joelau71 ... I tried the same thing. My problem is, when I click on close icon ('x') the change event is triggered. There I check if the ng-model is null or empty. If found empty then I set a default value which is not getting set. Can you help me?

I solved my problem by using the (clear) event. What I did:-

<ng-select name="data" [items]="mappedData"
    data-[(ngModel)]="code" bindLabel="name"
    bindValue="code" placeholder="Select Code"
    (clear)="resetCode()" data-(change)="codeChanged( code )"
    notFoundText="Code Not Found">

And in TS, I added the method:-

resetCode() {
    M.toast( { html: 'Setting by default the first Code', classes: 'red rounded' } );
    this.code = this.mappedData[0].code;
    this.codeChanged( this.code );
}

This worked perfectly as expected.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ratikant01 picture Ratikant01  路  4Comments

vincentjames501 picture vincentjames501  路  4Comments

mi1olson picture mi1olson  路  4Comments

tomasz-nowakowski picture tomasz-nowakowski  路  4Comments

shawnshaddock picture shawnshaddock  路  3Comments