mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 20:33:32 +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'); } ```
79 lines
1.6 KiB
TypeScript
79 lines
1.6 KiB
TypeScript
import {Directive, ElementRef, Optional, Input, HostListener} from 'angular2/core';
|
|
|
|
import {ViewController} from '../nav/view-controller';
|
|
import {Navbar} from '../navbar/navbar';
|
|
import {MenuController} from './menu-controller';
|
|
|
|
|
|
/**
|
|
* @name MenuToggle
|
|
* @description
|
|
* Toggle a menu by placing this directive on any item.
|
|
* Note that the menu's id must be either `leftMenu` or `rightMenu`
|
|
*
|
|
* @usage
|
|
* ```html
|
|
*<ion-content>
|
|
* <h3>Page 1</h3>
|
|
* <button menuToggle>Toggle Menu</button>
|
|
*</ion-content>
|
|
*
|
|
* ```
|
|
* @demo /docs/v2/demos/menu/
|
|
* @see {@link /docs/v2/components#menus Menu Component Docs}
|
|
* @see {@link ../../menu/Menu Menu API Docs}
|
|
*/
|
|
@Directive({
|
|
selector: '[menuToggle]',
|
|
host: {
|
|
'[hidden]': 'isHidden',
|
|
'menuToggle': '' //ensures the attr is there for css when using [menuToggle]
|
|
}
|
|
})
|
|
export class MenuToggle {
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
@Input() menuToggle;
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
private _inNavbar: boolean;
|
|
|
|
constructor(
|
|
private _menu: MenuController,
|
|
elementRef: ElementRef,
|
|
@Optional() private _viewCtrl: ViewController,
|
|
@Optional() private _navbar: Navbar
|
|
) {
|
|
this._inNavbar = !!_navbar;
|
|
|
|
// Deprecation warning
|
|
if (this._inNavbar && elementRef.nativeElement.tagName === 'A') {
|
|
console.warn('Menu toggles within a navbar should use <button menuToggle> instead of <a menu-toggle>')
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
@HostListener('click')
|
|
toggle() {
|
|
let menu = this._menu.get(this.menuToggle);
|
|
menu && menu.toggle();
|
|
}
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
get isHidden() {
|
|
if (this._inNavbar && this._viewCtrl) {
|
|
return !this._viewCtrl.isRoot();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|