mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 20:33:32 +08:00
refactor(menu): inject MenuController to control app menus
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'); } ```
This commit is contained in:
@ -1,9 +1,8 @@
|
||||
import {Directive, ElementRef, Optional, Input, HostListener} from 'angular2/core';
|
||||
|
||||
import {IonicApp} from '../app/app';
|
||||
import {ViewController} from '../nav/view-controller';
|
||||
import {Navbar} from '../navbar/navbar';
|
||||
import {Menu} from './menu';
|
||||
import {MenuController} from './menu-controller';
|
||||
|
||||
|
||||
/**
|
||||
@ -41,18 +40,18 @@ export class MenuToggle {
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
withinNavbar: boolean;
|
||||
private _inNavbar: boolean;
|
||||
|
||||
constructor(
|
||||
private _app: IonicApp,
|
||||
private _menu: MenuController,
|
||||
elementRef: ElementRef,
|
||||
@Optional() private _viewCtrl: ViewController,
|
||||
@Optional() private _navbar: Navbar
|
||||
) {
|
||||
this.withinNavbar = !!_navbar;
|
||||
this._inNavbar = !!_navbar;
|
||||
|
||||
// Deprecation warning
|
||||
if (this.withinNavbar && elementRef.nativeElement.tagName === 'A') {
|
||||
if (this._inNavbar && elementRef.nativeElement.tagName === 'A') {
|
||||
console.warn('Menu toggles within a navbar should use <button menuToggle> instead of <a menu-toggle>')
|
||||
}
|
||||
}
|
||||
@ -62,7 +61,7 @@ export class MenuToggle {
|
||||
*/
|
||||
@HostListener('click')
|
||||
toggle() {
|
||||
let menu = Menu.getById(this._app, this.menuToggle);
|
||||
let menu = this._menu.get(this.menuToggle);
|
||||
menu && menu.toggle();
|
||||
}
|
||||
|
||||
@ -70,7 +69,7 @@ export class MenuToggle {
|
||||
* @private
|
||||
*/
|
||||
get isHidden() {
|
||||
if (this.withinNavbar && this._viewCtrl) {
|
||||
if (this._inNavbar && this._viewCtrl) {
|
||||
return !this._viewCtrl.isRoot();
|
||||
}
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user