Jason
Jason

Reputation: 694

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

Upvotes: 17

Views: 8085

Answers (2)

Christoph B&#252;hler
Christoph B&#252;hler

Reputation: 2923

"$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

Upvotes: 8

Sajeetharan
Sajeetharan

Reputation: 222582

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

Upvotes: 33

Related Questions