1
私は主なコンポーネントとalertionic 3
のコンポーネントを持っています。イオン3角4警告コンポーネントは、アクションを実行するとコンポーネントに呼び出します
私がしようとしているのは、アラートコンポーネントの受け入れボタンがタップされるまで、メインコンポーネントを待機させることです。その後、メインコンポーネントはアラートコンポーネントのデータを取得する必要があります。
私の質問は、受け入れボタンタップを待っていることが、それを行うための良いアプローチであるかどうかです。
私はそれを行うにしようとするコードを貼り付けます。ここの下:
alertComponent.ts
import { Component } from '@angular/core';
import { AlertController } from 'ionic-angular';
@Component({
selector: 'unit-prompt'
})
export class UnitPromptComponent {
public quantity: number;
private hasAccepted: boolean = false;
constructor(
private alertCtrl: AlertController
) {}
public displayPrompt(): Promise<number> {
let prompt = this.alertCtrl.create({
title: 'Cantidad',
message: "Agrega el número de platos",
inputs: [{
name: 'title',
placeholder: 'Cantidad',
type: 'number'
}],
buttons: [{
text: 'Cancelar',
handler: data => {
console.log('Cancel clicked');
}
}, {
text: 'Agregar',
handler: data => {
this.setQuantity(data);
}
}]
});
prompt.present();
return this.checkIfPromptIsAccepted()
.then(() => {
return this.getQuantity();
});
}
public setQuantity(quantity) {
this.quantity = quantity;
this.hasAccepted = true;
}
private checkIfPromptIsAccepted() {
return new Promise((resolve, reject) => {
if (this.hasAccepted) {
resolve();
} else {
// trying to do a chained promise till this.hasAccepted is true
resolve(this.checkIfPromptIsAccepted());
}
});
}
public getQuantity() {
return this.quantity;
}
}
mainComponent.tsを
private doPrompt(dishName) {
this.prompt.displayPrompt()
.then((response) => {
console.log(response);
// do some stuff because the accept button has tapped
this.navCtrl.pop();
}).catch((e) => {
throw new Error('sth went wrong');
});
}
あなたがリンクしたドキュメントは、解雇後にルーティングのトピックについて文字通り話し合っています。彼らはもっと一般的な状況についても議論します。 –