ng-select Get object as selected value from

Guy E picture Guy E · May 3, 2018 · Viewed 11.6k times · Source

I'm using ng-select (NgSelectModule from '@ng-select/ng-select')ץ This is my code:

 <ng-container *ngFor="let validFilter of validFiltersData; let i = index" >
                <div *ngIf="validFilter.Values && validFilter.Values.length > 0" class=" filter col-md-4 ">
                        <ng-select [items]="validFilter.Values" bindLabel="Text" bindValue="ID" (change)= "onFilterValueChage($event, validFilter)" class="input-md" 
                        [(ngModel)]="validFilter.selectedValue"></ng-select>
                    </div>
            </ng-container>

validFiltersData is an array of FilterData:

export class FilterData 
{
    Name : string;
    FormattedName : string = null;
    Values : Filter[];
    selectedValue : Filter = null;
    ...
}

and Filter:

export class Filter 
{
    public ID: number;
    public Text: string;
    ....
}

I'm trying to get selected value as an object (Filter) that contains both ID and Text, but I always get only the ID.

How can I get the all Filter as selected value ?

Answer

CornelC picture CornelC · May 3, 2018

Because you set the binding to ID: bindValue="ID". Remove it and it should work.

Read more about bindings here: https://ng-select.github.io/ng-select#/bindings