22

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.

1
  • I ended up passing the popoverController instance from Home to BusinessDetails, and the using that instance to dismiss it. However, I'm not sure that's the best approach, or if there's a more straightforward method.
    – hugonne
    Commented Mar 5, 2019 at 18:32

2 Answers 2

47

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();
  }
4
  • 2
    Do you mean "to the component constructor"? Because that's where I need to dismiss it from, but it looks to me like doing that will inject a new instance of the controller, so would not be dismissing the original one. I'll give it a try, anyway. Thanks.
    – hugonne
    Commented Mar 5, 2019 at 19:58
  • 1
    yes the component constructor,sorry my mistake.let me know if you succeeded. good luck Commented Mar 5, 2019 at 20:03
  • It did work, thanks for the help. So, that means that the PopoverController you inject is actually a sinlgeton, correct? Otherwise, why would it close the instance injected to the constructor of the Home page?
    – hugonne
    Commented Mar 5, 2019 at 20:32
  • Thank you, it helped me . Very strange, but the first time it did not work. When I tried this method after a while, it started working O_o
    – Денис
    Commented Mar 11, 2020 at 23:51
4

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();
}
2
  • Wrong. you are taking input as 'onClick' and on 'afterClick' writing 'this.click()' Commented Mar 10, 2020 at 13:17
  • Kudos, very clever indeed! Commented Mar 24, 2021 at 10:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.