简体   繁体   English

如何设置默认值离子选择项?

[英]How to set default value ion-select item?

I need to set default value to ion-select item.我需要为离子选择项目设置默认值。

在此处输入图片说明

The ion-select above contain list of places, After choosing a place I save it into a localStorage.上面的 ion-select 包含地点列表,选择一个地点后,我将其保存到 localStorage 中。

What I need this localStorage be the default value of the ion-select,like this:我需要这个 localStorage 是离子选择的默认值,如下所示:

在此处输入图片说明

When the page is changed the ion-select return the placeHolder and no item is selected当页面更改时,离子选择返回 placeHolder 并且没有选择任何项目

CODE :代码

  <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>

I found a solution to that.. add a AbstractControl to your [(ngModel)].我找到了一个解决方案..将 AbstractControl 添加到您的 [(ngModel)]。

example:例子:
in yout TS file..在你的 TS 文件中..

placeForm: FormGroup;
placeId: AbstractControl;

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

in your html just add this [(ngModel)]="placeForm.placeId"在你的 html 中添加这个[(ngModel)]="placeForm.placeId"

If you have the index value you can just fetch that from localstorage and when the page loads you can save it in a variable and then use it to bind to the selected property on ion-option:如果你有索引值,你可以从 localstorage 中获取它,当页面加载时你可以将它保存在一个变量中,然后使用它绑定到 ion-option 上的选定属性:

html: html:

<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" [selected]="i == selectedIndex" value="{{place.id}}"> // the variable selected index will be a number and will select the item matching with the index from the array.
    {{place.name}}
    </ion-option>
  </ion-select>
</ion-item>

in ts:在 ts:

selectedIndex: number;

constructor(){
  this.selectedIndex = localStorage.getItem('index') // 
}

I solved我解决了

the ion-select have the two way data binding with placeId离子选择具有与placeId双向数据绑定

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

After Selecting a place I saved the id of the selected place inside the local storage.选择一个地点后,我将所选地点的 id 保存在本地存储中。 Then on the ionViewDidLoad() I init the the variable this.placeId然后在ionViewDidLoad()我初始化变量this.placeId

this.placeId = localstorage.getItem('foo')

and it work properly.它工作正常。

My mistake: I was saving the name of the place in the localstorage and not the id of the place我的错误:我在 localstorage 中保存了地点的名称,而不是地点的 id

Thanks谢谢

In ionic 4在离子 4

  ionViewDidEnter() {
    let parametros = this.navParams.get('parametros');
    this.cuentaBancaria.empresa._id = parametros.empresa;
    this.cuentaBancaria.moneda._id = parametros.moneda;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM