mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-21 21:15:24 +08:00

Menu has been improved to make it easier to open, close, toggle and enable menus. Instead of injecting `IonicApp` to find the menu component, you now inject `MenuController`. Was: ``` constructor(app: IonicApp) { this.app = app; } openMenu() { this.app.getComponent('leftMenu').close(); } ``` Now: To programmatically interact with any menu, you can inject the `MenuController` provider into any component or directive. This makes it easy get ahold of and control the correct menu instance. By default Ionic will find the app's menu without requiring a menu ID. An id attribute on an `<ion-menu>` is only required if there are multiple menus on the same side. If there are multiple menus, but on different sides, you can use the name of the side to get the correct menu If there's only one menu: ``` constructor(menu: MenuController) { this.menu = menu; } openMenu() { this.menu.close(); } ``` If there is a menu on the left and right side: ``` toggleMenu() { this.menu.toggle('left'); } ``` If there are multiple menus on the same side: ``` <ion-menu id="myMenuId" side="left">...</ion-menu> <ion-menu id="otherMenuId" side="left">...</ion-menu> closeMenu() { this.menu.close('myMenuId'); } ```
72 lines
1.5 KiB
TypeScript
72 lines
1.5 KiB
TypeScript
import {App, IonicApp, MenuController, Page, NavController, Alert} from 'ionic/ionic';
|
|
|
|
|
|
@Page({
|
|
templateUrl: 'page1.html'
|
|
})
|
|
class Page1 {
|
|
constructor(private nav: NavController) {}
|
|
|
|
presentAlert() {
|
|
let alert = Alert.create({
|
|
title: "New Friend!",
|
|
message: "Your friend, Obi wan Kenobi, just accepted your friend request!",
|
|
cssClass: 'my-alert',
|
|
buttons: ['Ok']
|
|
});
|
|
this.nav.present(alert);
|
|
}
|
|
}
|
|
|
|
|
|
@Page({templateUrl: 'page3.html'})
|
|
class Page3 {}
|
|
|
|
|
|
@Page({templateUrl: 'page2.html'})
|
|
class Page2 {
|
|
constructor(private nav: NavController) {}
|
|
|
|
page3() {
|
|
this.nav.push(Page3);
|
|
}
|
|
}
|
|
|
|
|
|
@App({
|
|
templateUrl: 'main.html'
|
|
})
|
|
class E2EApp {
|
|
|
|
constructor(private app: IonicApp, private menu: MenuController) {
|
|
this.rootView = Page1;
|
|
this.changeDetectionCount = 0;
|
|
|
|
this.pages = [
|
|
{ title: 'Page 1', component: Page1 },
|
|
{ title: 'Page 2', component: Page2 },
|
|
{ title: 'Page 3', component: Page3 },
|
|
];
|
|
}
|
|
|
|
openPage(page) {
|
|
// Reset the content nav to have just this page
|
|
// we wouldn't want the back button to show in this scenario
|
|
let nav = this.app.getComponent('nav');
|
|
nav.setRoot(page.component).then(() => {
|
|
// wait for the root page to be completely loaded
|
|
// then close the menu
|
|
this.menu.close('left');
|
|
});
|
|
}
|
|
|
|
onMenuOpening(ev) {
|
|
console.log('onMenuOpening', ev);
|
|
}
|
|
|
|
isChangeDetecting() {
|
|
console.log('Change detection', ++this.changeDetectionCount);
|
|
return true;
|
|
}
|
|
}
|