angularangular-uiangular-ngselectangular13angular-dragdrop

Display selected value from drop down list in Angular 13


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


Solution

  • 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>