gpt4 book ai didi

html - 如何设置 ionic 选择项的默认值?

转载 作者:行者123 更新时间:2023-12-04 16:06:16 25 4
gpt4 key购买 nike

我需要为 ionic 选择项设置默认值。

enter image description here

上面的 ion-select 包含位置列表,选择一个位置后我将其保存到 localStorage 中。

我需要这个 localStorage 是 ion-select 的默认值,像这样:

enter image description here

When the page is changed the ion-select return the placeHolder and no item is selected

代码:

  <ion-item>
<ion-select multiple="false" placeholder="Which Place?" [(ngModel)]="placeId" (ionChange)="showPlace()">
<ion-option class="text-input place-field" *ngFor="let place of places | async;let i = index" value="{{place.id}}">
{{place.name}}
</ion-option>
</ion-select>
</ion-item>

最佳答案

我找到了一个解决方案..将 AbstractControl 添加到您的 [(ngModel)]。

例子:
在你的 TS 文件中..

placeForm: FormGroup;
placeId: AbstractControl;

constructor(){
this.placeForm.formBuilder.group({
placeId: ['', Validators.required],
});
this.placeForm.get('placeId').setValue(place.id);
}

在你的 html 中只需添加此 [(ngModel)]="placeForm.placeId"

关于html - 如何设置 ionic 选择项的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48703824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com