Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting $mdMenu into scope. Cannot read property open() of undefined

I am trying to get the Angular Material Design menu to work but I don't seem to be able to use the $mdMenu that is supposed to be injected by the ng-click.

My HTML markup:

<div layout="column" layout-fill ng-controller="AuthControl">
    <md-toolbar ng-controller="navigationControl">
        <div ng-controller="menu as ctrl">
          <md-menu>
             <md-button class="md-icon-button" ng-click="ctrl.open($mdMenu, $event)">
               <md-icon>menu</md-icon>
             </md-button>
             <md-menu-content width="4">
                <md-menu-item>
                   <md-button>
                       <md-icon>account_circle</md-icon>
                   </md-button>
                </md-menu-item>
             </md-menu-content>
         </md-menu>
      </div>
   </md-toolbar>
</div>

The Angular Controller:

controllers.controller('menu', function menuControl($mdDialog) {
   var originatorEv;

   this.open = function($mdMenu, ev) {
     originatorEv = ev;
     $mdMenu.open(ev);
   };
});

The contoller gets injected properly but when I run I get the Error

TypeError: Cannot read property 'open' of undefined

Does anybody know how to fix this? Thanks

like image 836
Jason Avatar asked Feb 04 '17 15:02

Jason


2 Answers

Instead of mdMenu, pass mdOpenMenu

  <md-button aria-label="menu" class="md-fab md-mini md-primary" ng-click="ctrl.openMenu($mdOpenMenu, $event)">

Controller:

 this.openMenu = function($mdOpenMenu, ev)     {
      originatorEv = ev;
      $mdOpenMenu(ev);
    };

DEMO

like image 157
Sajeetharan Avatar answered Sep 21 '22 06:09

Sajeetharan


"$mdOpenMenu" has been replaced by "$mdMenu.open" and is now deprecated. Use the latest version of Angular Material and it will work just fine.

enter image description here

like image 37
Christoph Bühler Avatar answered Sep 22 '22 06:09

Christoph Bühler