I have an array of objects(named users) which will be shown as options of dropdownlist
. and I have another objects list(named selectedUsers and is saved in backend) which is used to initialize the dropdownlist
.
array:
users = [
{
id: 2,
name: 'name2'
},{
id: 2,
name: 'name2'
},{
id: 3,
name: 'name3'
}
];
selectedUsers3 = [
{
id: 1,
name: 'name1'
},{
id: 2,
name: 'name2'
}
];
I'm facing a wired situation which is when I bind Object
to select options
by [ngValue]
, and bind a function to [selected]
which will check whether the current option exists in selectedUsers
.
I can see the function is retrieved and the result is returned true/false as excepted, but the options keeps unselected.
template:
<select multiple [(ngModel)]="selectedUsers3">
<option *ngFor="let user of users" [selected]="checkExist(user)" [ngValue]="user">{{user.name}}</option>
</select>
function in component:
checkExist(user) {
return this.selectedUsers3.findIndex(selUser => selUser.id === user.id) > -1;
//return this.selectedUsers3.filter(selUser => selUser.id === user.id).length > 0;
}
mention that I used Array.filter
or Array.findIndex
to check whether the data exists, and the result is correct.
Please refer this demo with the third dropdownlist
, and check where am I doing something wrong? or am I missing something about [selected]
? I hope someone can explain clearly about this.
UPD:
with @Günter Zöchbauer's help, this situation can be solved by using compareWith
directive(refer his answer) no matter single select
or multi select
, but I'm still confused why they work well alongside but fail together and still trying to figure out the reason.