Display selected value from drop down list in Angular 13

2.8k views Asked by At

Here's my code:

<tbody *ngFor="let user of users">
    <td>{{user.name}}:</td>
    <td>
        <ng-select  [items]="userList" bindValue="id" formControlName='id' bindLabel="label">
            <ng-template ng-option-tmp let-item="item">
                <div [ngbTooltip]="item.label" placement="bottom">{{item.label}}</div>
            </ng-template>
        </ng-select>
    </td>
</tbody>

userList: [
    { id: 1, label: 1 },
    { id: 2, label: 2 }
]

users: [
    { id: 5545, name: 'Z', value: 1 }
    { id: 1112, name: 'A', value: 2 },
    { id: 4455, name: 'B', value: 1 }
]

If userList.id == user.value then it's label should be selected in drop down.

Current Result:

enter image description here

Expected Result:

enter image description here

1

There are 1 answers

0
Sahil Thummar On BEST ANSWER

Found the solution.

formControlName='{{user.value}}' Instead of formControlName='id' works for me.

<tbody *ngFor="let user of users">
    <td>{{user.name}}:</td>
    <td>
        <ng-select  [items]="userList" bindValue="id" formControlName='{{user.value}}' bindLabel="label">
            <ng-template ng-option-tmp let-item="item">
                <div [ngbTooltip]="item.label" placement="bottom">{{item.label}}</div>
            </ng-template>
        </ng-select>
    </td>
</tbody>