I have a standard Ionic 4 page (Home) that creates a popover that uses a component (BusinessDetails) with a button that redirects to a new page (RequestTurn). However, when I click on that button, the popover is not dismissed and is renders on top of my RequestTurn page. I guess I need to manually dismiss it from the component (BusinessDetails), but I don't know how to access the instance of the popover from there, because it was created in the Home page. Is there a way to do this?
home.page.ts
presentModal(business:Business, event: Event) {
this.popoverController.create(({
component: BusinessDetailsComponent,
cssClass: "business-popover",
showBackdrop: true,
componentProps: {
business: business
}
}) as any).then(popover => popover.present()); }
business-detail.component.ts
goToRequestTurn(id: string) {
//Need to dismiss popver here (?)
this.router.navigateByUrl(`/request-turn/${id}`); }
Thanks for your help.
To dismiss the popover after creation, call the dismiss() method on the Popover instance. The popover can also be dismissed from within the popover's view by calling the dismiss() method on the ViewController. The dismiss() call accepts an optional parameter that will be passed to the callback described as follows.
Presenting. To present a popover, call the present method on a popover instance. In order to position the popover relative to the element clicked, a click event needs to be passed into the options of the the present method. If the event is not passed, the popover will be positioned in the center of the viewport.
There are two ways to use ion-popover : inline or via the popoverController .
When using ion-popoverwith Angular, React, or Vue, the component you pass in will be destroyed when the popover is dismissed. As this functionality is provided by the JavaScript framework, using ion-popoverwithout a JavaScript framework will not destroy the component you passed in.
To dismiss the popover after creation, call the dismiss () method on the Popover instance. The popover can also be dismissed from within the popover's view by calling the dismiss () method on the ViewController. The dismiss () call accepts an optional parameter that will be passed to the callback described as follows.
ion-popovercan be used by writing the component directly in your template. This reduces the number of handlers you need to wire up in order to present the popover. When using ion-popoverwith Angular, React, or Vue, the component you pass in will be destroyed when the popover is dismissed.
isOpenuses a one-way data binding, meaning it will not automatically be set to falsewhen the popover is dismissed. Developers should listen for the ionPopoverDidDismissor didDismissevent and set isOpento false. The reason for this is it prevents the internals of ion-popoverfrom being tightly coupled with the state of the application.
add private popoverController: PopoverController
to the component constructor
then write a function like this and call it when you want to dismiss the modal
async DismissClick() {
await this.popoverController.dismiss();
}
I solved this problem as follows: In parent component I have passed callback as prop to child component:
const popover = await this.popoverController.create({
component: PopoverComponent,
event: ev,
componentProps: {
onClick: () => {
popover.dismiss();
},
},
});
await popover.present();
And in PopoverComponent I have added @Input() onClick;
which called when the user clicks:
...
@Input()
public onClick = () => {}
...
afterClick() {
this.onClick();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With