将$ mdMenu注入范围。无法读取未定义

时间:2017-02-04 15:05:36

标签: javascript angularjs angular-material

我正在尝试使用Angular Material Design菜单,但我似乎无法使用应该通过ng-click注入的$ mdMenu。

我的HTML标记:

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

角度控制器:

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

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

控制器被正确注入但是当我运行时我得到了错误

TypeError: Cannot read property 'open' of undefined

有人知道如何解决这个问题吗? 感谢

2 个答案:

答案 0 :(得分:30)

而不是 mdMenu ,传递 mdOpenMenu

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

<强>控制器:

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

<强> DEMO

答案 1 :(得分:6)

&#34; $ mdOpenMenu&#34; has been replaced由&#34; $ mdMenu.open&#34;现已弃用。使用最新版本的Angular Material,它可以正常工作。

enter image description here