[英]How to dismiss LoadingController once a method inside is complete?
我在下面創建一個LoadingController
,展示它,並執行一些方法:
this.loadingCtrl.create({
message: 'Sending Message...'
}).then(loadingEl => {
loadingEl.present();
this.conversationsService.addMessageToConversation(this.conversation.id, this.form.value.message);
this.loadMsg();
this.form.reset();
});
一旦addMessageToConversation()
成功,我就想dismiss
LoadingController。
有人可以告訴我我該怎么做嗎?
如果需要,這里是addMessageToConversation()
:
addMessageToConversation(conversationId: string, message: string) {
this._conversations.getValue().find(conversation => conversation.id === conversationId)
.messages.push(
new Message(
Math.random().toString(),
message,
this.authService.userId,
new Date(Date.now())
));
}
有兩種方法可以做到這一點。
我建議使用方法#2,因為那是“正常的”。 將服務的方法轉換為 promise 后,您可以按如下方式調整代碼:
this.loadingCtrl.create({ message: 'Sending Message...' }).then(async loadingEl => { loadingEl.present(); const res = await this.conversationsService.addMessageToConversation(this.conversation.id, this.form.value.message); this.loadMsg(); this.form.reset(); this.loadingCtrl.dismiss() });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.