Search code examples
angularionic-frameworkionic4ionic-native

Hide any div or text when ion-menu is opened in Ionic 5 using Angular


I want to hide the toolbar .custom-nav-link div when the ionic side menu will open and again show this when the side menu is closed. How to detect and apply conditions in HTML based on ion-menu is open or close?

enter image description here

<ion-toolbar>
  <div class="containers">
    <div class="logo" >
      <h4>My Toolbar</h4>
    </div>

    <div class="custom-nav-link" slot="end">
      <nav class="desktop-nav">
        <ul class="nav-list">
          <li *ngFor="let p of buttonLinks">
            <a href="">{{p.title}}</a>
          </li>
        </ul>
      </nav>
    </div>

    <div class="">
      <ion-buttons slot="start">
        <ion-menu-button menu="userMenu">
          <ion-icon name="apps"></ion-icon>
        </ion-menu-button>
      </ion-buttons>
    </div>

  </div>
</ion-toolbar>

Solution

  • Try this:

    import { MenuController } from '@ionic/angular';
    
    public isMenuOpen: boolean = false;
    
    constructor(public menuCtrl:MenuController){}
    
    ngOnInit() {   // or on click of menu button
        this.menuCtrl.isOpen('userMenu').then(res => {
            this.isMenuOpen = res;
        })
    }
    

    and html

    <div *ngIf="!isMenuOpen" class="custom-nav-link" slot="end">
      <nav class="desktop-nav">
        <ul class="nav-list">
          <li *ngFor="let p of buttonLinks">
            <a href="">{{p.title}}</a>
          </li>
        </ul>
      </nav>
    </div>
    
    <div class="">
      <ion-buttons slot="start">
        <ion-menu-button menu="userMenu">
          <ion-icon name="apps"></ion-icon>
        </ion-menu-button>
      </ion-buttons>
    </div>