Issue
ngValue will not bind the value unless you keep the reference of same list. is it correct ?
This will not work
this.myForm.get('user').patchValue(this.currentUser);
This will work
const findIndex = this.user.findIndex(
(item) => item.id == this.currentUser.id
);
this.myForm.get('user').patchValue(this.user[findIndex]);
Also i came to one more property compareWith that can be used to bind as well.
This will work if i use it with compareWith.
this.myForm.get('user').patchValue(this.currentUser);
Playground Link: https://stackblitz.com/edit/angular-ivy-a4zbhn?file=src%2Fapp%2Fapp.component.ts
Solution
You should use [compareWith]
Input property binding to the <select>
element so that Angular could bind the value to the <select>
by your defined comparison logic.
.component.html
<select formControlName="user" [compareWith]="compare">
<option *ngFor="let us of user" [ngValue]="us">{{ us.name }}</option>
</select>
.component.ts
compare(val1, val2) {
return val1.id === val2.id;
}
Updated Remarks:
As Angular - SelectControlValueAccessor (Customizing option selection section) written,
Angular uses object identity to select option.
Without [compareWith]
, by default Angular will use object reference to select the option.
Hence your inference with the first method is correct and workable as long you are providing the object with the exact reference as the object(s) is existed in [ngValue]
.
const findIndex = this.user.findIndex(
(item) => item.id == this.currentUser.id
);
this.myForm.get('user').patchValue(this.user[findIndex]);
References
Answered By - Yong Shun
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.