我在使用angular 8的ionic 4应用程序中遇到了一个问题。反应式表单)通过呈现ion-select
不幸的是,我的null值选项没有被识别为ion-select的选定值,即使ion-select的值(null)显示为可能的ion-select-option。
我的组件如下所示:
export class MyComponent {
public formGroup;
public types = [
{
value: null,
title: 'All'
},
{
value: 'Type-1',
title: 'Type 1'
},
{
value: 'Type-2',
title: 'Type 2'
}
];
constructor(private fb: FormBuilder) {
this.formGroup = this.fb.group({
type: [null]
});
}
}此组件的模板如下所示:
<ion-content>
<form [formGroup]="formGroup">
<ion-list>
<ion-item>
<ion-label position="floating">Type</ion-label>
<ion-select formControlName="type" interface="popover">
<ion-select-option *ngFor="let type of types" [value]="type.value">
{{ type.title }}
</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
</form>
</ion-content>在ion-select上为空值使用[placeholder]的变通方法对我的场景没有什么用处,因为我希望用户仍然可以使用null选项,即使之前选择了其他选项。
有没有解决这个问题的方法,或者我在使用in 4/reactive forms的时候做错了什么?
发布于 2021-07-28 17:45:39
我也有同样的问题,我尝试了一个使用compareWith函数的变通方法:
<ion-select ... [compareWith]="compareWithFn">
public compareWithFn(value1, value2) {
// value1 option value, value2 - control value
if (value1 === null && !value2) {
return true;
}
return value1 === value2;
}这帮助我选择了“全部”选项。
https://stackoverflow.com/questions/59050173
复制相似问题