prachi
prachi

Reputation: 129

How to use key-Value pairs in ng-select in angular 8

I want to display a dropdown for a key-value pair array. It should display values with bindLabel as the value and the value binded that is bindValue should be the key.

For example : if array is:

Array= {
"key1": "value1",
"key2": "value2",
   .
   .
   .
"key3":"value3"

}

the dropdown should be displayed as value1, value2 , value3 and so on. And if we select value1, the binded value should be key1.

I am using :

<ng-select [items]="Array" [searchable]="true" >
  <ng-option [value]="">Select</ng-option>
</ng-select>

It is showing error as: ERROR TypeError: items.map is not a function.

I want to do this using ng-select. Is it possible to achieve this .

Upvotes: 1

Views: 2932

Answers (1)

Use keyvalue pipe

<ng-select [items]="Array | keyvalue" [bindValue]="'key'" [bindLabel]="'value'" [searchable]="true"></ng-select>

Upvotes: 2

Related Questions