我已经建立了一种将自定义文本插入到离子选择多重中的方法。我使用警报框将自定义测试插入选择数组,并在关闭警报框时在输入字段中输入。一切正常,除了将数据插入输入字段后,警报框将再次打开。.如何确保异步inputCustomValuesMulti仅运行一次。
html
<ion-item class="ion-text-wrap">
<ion-label position="stacked">{{fromItem.Question}}</ion-label>
<ion-select multiple="true" #c (ionChange)="selectChangedMulti(c.value, i)" name="{{fromItem.name}}" [(ngModel)]="fromItem.input">
<ion-select-option *ngFor="let item of importQuestions[i].values" value="{{item.value}}" >{{ item.value }}</ion-select-option>
</ion-select>
</ion-item>
ts
selectChangedMulti(selectedValue: string[], index: string ) {
this.index = index;
if (selectedValue.includes("other")) {
this.inputCustomValuesMulti(selectedValue);
} else {
this.currentValue = selectedValue;
};
};
async inputCustomValuesMulti(selectedValue) {
const inputAlert = await this.alertController.create({
header: 'Enter your custom text:',
inputs: [ { type: 'text', placeholder: 'type in' } ],
buttons: [ { text: 'Cancel' }, { text: 'Ok' } ]
});
await inputAlert.present();
await inputAlert.onDidDismiss().then((data) => {
let customValueName: string = data.data.values[0];
if (customValueName) {
let indexFound = this.importQuestions[this.index].values.findIndex((item: string) => item === customValueName)
if (indexFound === -1) {
this.importQuestions[this.index].values.push({value: customValueName});
let importQuestion = selectedValue;
importQuestion.push(customValueName);
let passData = importQuestion;
let newArray = Array.from(passData);
// inserts new text into input field
this.importQuestions[this.index].input = newArray;
} else {
console.log("else");
};
};
})
};
let passData = importQuestion.filter(e => e !== "other");
现在方法不再在ionChange上看到“其他”。