Ionic 5, following the documentation for ion-select “Objects as Values”. The example is straight up broken.
Page html
<ion-select [compareWith]="compareWith">
<ion-select-option *ngFor="let category of this.categories">{{category.name}}</ion-select-option>
</ion-select>
Category.ts
export class Category {
guid: string;
name: string;
constructor(guid, name) {
this.guid = guid;
this.name = name;
}
}
Page.ts
export class TaskPage implements OnInit {
categories: Array<Category>;
constructor() { }
ngOnInit() {
this.categories = new Array<Category>();
this.categories.push(new Category("1", "Test 1"));
this.categories.push(new Category("2", "Test 2"));
}
compareWithFn = (o1, o2) => {
return o1 && o2 ? o1.guid === o2.guid : o1 === o2;
};
compareWith = this.compareWithFn;
}
First off, I had to change the “compareWith = compareWithFn” to include the “this.”
Second, and more importantly, when I select an option, it doesn’t display what I selected and then if I try to select again, every option is selected.
This is after I selected option “Test 2”