Ionic framework 离子4:如何更新或刷新离子选择脚本?
我用ionic 4和angular创建了一个注册表单的应用程序。我想做以下工作: 我有两个离子选择,一个是国家列表,另一个是电话号码列表。我正在寻找的行为是:当在ion select for countries中选择一个国家时,必须在电话代码ion select中自动选择相应的电话代码 这是我的代码: 离子选择国家Ionic framework 离子4:如何更新或刷新离子选择脚本?,ionic-framework,ionic4,Ionic Framework,Ionic4,我用ionic 4和angular创建了一个注册表单的应用程序。我想做以下工作: 我有两个离子选择,一个是国家列表,另一个是电话号码列表。我正在寻找的行为是:当在ion select for countries中选择一个国家时,必须在电话代码ion select中自动选择相应的电话代码 这是我的代码: 离子选择国家 <ion-select id="paisselect" formControlName="pais" (ionChange)="changeselect($event)"
<ion-select id="paisselect" formControlName="pais" (ionChange)="changeselect($event)" interface="popover">
<ion-select-option *ngFor="let c of countries" value="{{c.cod}}">{{c.name}}</ion-select-option>
</ion-select>
我使用变量“codigoselected”来确定选择哪个国家。为此,在离子选择选项中,我使用一个条件更改选项的选定状态,如下所示:
<ion-select-option *ngFor="let c of codetele" value="{{c.code}}" selected="{{codigoselected == c.code}}">{{c.code}}</ion-select-option>
{{c.code}
当前行为如下所示:
<ion-select-option *ngFor="let c of codetele" value="{{c.code}}" selected="{{codigoselected == c.code}}">{{c.code}}</ion-select-option>
我选择了一个国家:
但是ion select的电话号码似乎没有更新
但是,当我单击该字段时,特定代码被选中
我需要在选择选项下执行clic,以便刷新字段:
那么,如何在用户不点击的情况下使界面中的电话代码字段自动更新?尝试此操作,将电话代码选择元素传递给您的
更改选择(事件,电话代码选择)
功能
<ion-select id="paisselect" formControlName="pais" (ionChange)="changeselect($event, phoneCodeSelect)" interface="popover">
<ion-select-option *ngFor="let c of countries" value="{{c.cod}}">{{c.name}}</ion-select-option>
</ion-select>
然后在函数中手动更改select元素的值,同时更新phoneCode表单控件值
changeselect($event, phoneCodeSelect){
console.log($event.target.value) ;
let item1 = this.countries.find(i => i.cod === $event.target.value);
console.log(item1) ;
this.yourFormGroup.value.phonecode = item1.code; // change yourFormGroup to what your variable's name is
phoneCodeSelect.value = item1.code;
}
为什么不更新表单控件值?而不是
this.codigoselected=item1.code
可能类似于this.yourFormGroup.value.phonecode=item1.code
。
<ion-select #phoneCodeSelect formControlName="phonecode" placeholder="Cod." interface="popover">
<ion-select-option *ngFor="let c of codetele" value="{{c.code}}" selected="{{codigoselected == c.code}}">{{c.code}}</ion-select-option>
</ion-select>
changeselect($event, phoneCodeSelect){
console.log($event.target.value) ;
let item1 = this.countries.find(i => i.cod === $event.target.value);
console.log(item1) ;
this.yourFormGroup.value.phonecode = item1.code; // change yourFormGroup to what your variable's name is
phoneCodeSelect.value = item1.code;
}