Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle Menu in Ionic 4

I am new to Ionic, and I am developing a basic app in Ionic 4 with sidemenu.

I am adding a button in Side Menu, when I am clicking on that button the menu is not toggling. can anyone help me in this ? here's the code which I am trying.

<ion-button color="primary" (click)="function()" class="class" menuToggle expand="block" fill="outline"> text </ion-button>

Explanation of not Duplicate question

Please check the Bold text, I already have a working sidemenu, but the problem is I want to close the sidemenu when I click on ion-button, not on ion-item.

like image 562
Riot Zeast Captain Avatar asked Dec 16 '18 16:12

Riot Zeast Captain


People also ask

How do I use the ion menu toggle?

The MenuToggle component can be used to toggle a menu open or closed. By default, it's only visible when the selected menu is active. A menu is active when it can be opened/closed. If the menu is disabled or it's being presented as a split-pane, the menu is marked as non-active and ion-menu-toggle hides itself.

How do you add a menu in ion 4?

In the page you want to show your side menu, you can use the ion-menu-button tag. Set the autoHide tag to false, so you will always see the menu button.

How do you close the side menu in Ionic 4?

The menuClose directive can be placed on any button to close an open menu.

How do I open the side menu in Ionic?

Note: ion-menu-toggle is used to open and close the side menu, therefore when you click on a menu item, it will close the side menu automatically.


1 Answers

Simply encapsulate your ion-button within an ion-menu-toggle element, like so:

<ion-menu-toggle>
  <ion-button>Toggle Menu</ion-button>
</ion-menu-toggle>

View the documentation here

EDIT: If you don't want to use ion-menu-toggle, you can do this instead:

In your app.component.ts:

import { MenuController } from '@ionic/angular'; //import MenuController to access toggle() method.

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {
  constructor(
    public menuCtrl: MenuController
  ) {
    this.initializeApp();
  }

toggleMenu() {
    this.menuCtrl.toggle(); //Add this method to your button click function
  }

}

And in your app.component.html:

<ion-button (click)="toggleMenu()">Toggle Menu</ion-button>

To view all methods, check the docs here.

like image 65
NickyTheWrench Avatar answered Sep 19 '22 15:09

NickyTheWrench