Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss Ionic 4 popover from its component

Tags:

popover

ionic4

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.

like image 318
hugonne Avatar asked Mar 05 '19 18:03

hugonne


People also ask

How do you dismiss popover in ionic 4?

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.

How do I change the popover position in ionic?

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.

How many ways can you add the popover in ionic framework?

There are two ways to use ion-popover : inline or via the popoverController .

Does ion-popover destroy the component I passed in?

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.

How do I dismiss the Popover after it is created?

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.

How do I use ion-popover?

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.

Why is the ionpopoverdiddismissor set to false?

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.


2 Answers

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();
  }
like image 182
Ehsan Kiani Avatar answered Sep 18 '22 13:09

Ehsan Kiani


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();
}
like image 44
Денис Avatar answered Sep 20 '22 13:09

Денис