0

i have a popover which i want to close on click of anywhere on document

here is my demo: https://stackblitz.com/edit/ionic-3bw7da?file=pages%2Fhome%2Fhome.html

right now it is not closing as i don't know how to close it.

enter image description here

here is my demo:https://stackblitz.com/edit/ionic-3bw7da?file=pages%2Fhome%2Fhome.html

please help me thanks in advance!!!

  • 2
    import `PopoverOptions` and use `create(component, data,{enableBackdropDismiss: true, showBackdrop:true})` – Mohan Gopi Oct 25 '17 at 08:41

2 Answers2

0

I answered a similar question, you have to add a listener to your component, thanks to the host property. Inject ElementRef in your constructor called "element" in this example.

constructor(private element: ElementRef)

@Component({
     selector..., 
     host: {'(document:click)': 'onClick($event)',}
});


onClick(event) {
if (!this.element.nativeElement.contains(event.target)) {
      closeModal(); // or not
    }
}

I answered the question here : Angular, resume event propagation

andrea06590
  • 1,259
  • 3
  • 10
  • 22
  • 2
    if you answered a similar question, I suggest to update that as a comment. – Aravind Oct 25 '17 at 08:55
  • @Westi-Tech, it is not working for me can make a demo –  Oct 25 '17 at 09:26
  • Saved (I had to connect with Github) https://stackblitz.com/edit/ionic-uu8jkf I created for you the menu component where you have to manage every event related to the menu. You must separate as much as possible your component. – andrea06590 Oct 25 '17 at 11:49
  • @Westi-Tech, this https://stackblitz.com/edit/ionic-uu8jkf?file=pages%2Fhome%2Fhome.ts result is accurate but i did not see any code added the please help me which code you have added –  Oct 25 '17 at 12:24
  • Check again, i can see now in pages/home you have a new directory called menu ;) i've created it and added it to your app module – andrea06590 Oct 25 '17 at 12:39
  • @Westi-Tech, it is working but `onclick` on anywhere on the `document` it is showing ? why ? please fix that –  Oct 25 '17 at 14:03
  • It's very easy to fix man, you should retry the angular 2 getting started. Check your live editor https://stackblitz.com/edit/ionic-uu8jkf, i fixed it using 2 different functions close and open menu. Close menu just change the variable if true ;) – andrea06590 Oct 25 '17 at 14:11
0

I did not succeed to format my code in a comment :/

import { Component, ElementRef } from '@angular/core';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  host:{'(document:click)': 'onClick($event)',
}
})

export class HomePage {

    constructor(public navCtrl: NavController, private element: ElementRef) {

  }

  onClick(event) {
   // display event in console if outside of menu top bar
   if (this.element.nativeElement.contains(event.target)) {
     console.log(event);
     this.closeMenu();    
   } else {
    this.openMenu();    
   }

  openMenu() {
    this.showMoreBarItem = !this.showMoreBarItem;      
  }

  closeMenu() {
    if(this.showMoreBarItem) {
      this.showMoreBarItem = !this.showMoreBarItem;      
    }
  }}

IMPORTANT : You have to separate your menu component to accomplish it, and only apply my changes in your menu component => in your case create a new component in home.html for instance !

andrea06590
  • 1,259
  • 3
  • 10
  • 22
  • i have tried it but it is not working ,**please try here** https://stackblitz.com/edit/ionic-3bw7da?embed=1&file=pages/home/home.html&view=editor –  Oct 25 '17 at 09:52
  • Are you done with it ? Let me know if you want to reduce the click to the 3 bullets icon only instead of the full top menu. If so, change the behavior of your menu (reduce the width in initial state to reduce the click area) and increase it if clicked (dropdown menu) – andrea06590 Oct 25 '17 at 11:53