material icon indicating copy to clipboard operation
material copied to clipboard

Menu bar is not working when using ng-repeat

Open Jayaraj8905 opened this issue 11 years ago • 11 comments

I built the menu bar using ng-repeat angular directive. In this scenario menu bar is not working. But when i hard code without using ng-repeat, its working perfectly. Seems there will be a problem in menu bar when using ng-repeat.

Jayaraj8905 avatar Sep 07 '15 09:09 Jayaraj8905

Please provide a CodePen or Plunkr that demonstrates this issue. Here are some starter demo templates that you can use/fork:

  • CodePen template: http://codepen.io/team/AngularMaterial/pen/BNYppz
  • Plunkr template: http://plnkr.co/edit/yP0gB9iTacIQb48thud8?p=preview.

ThomasBurleson avatar Sep 07 '15 18:09 ThomasBurleson

Here is the sample code: http://codepen.io/anon/pen/BoNGyN When i opened the level 1_1 menu and then try to open the level 1_2 menu, the previous didnt get close.

Jayaraj8905 avatar Sep 09 '15 04:09 Jayaraj8905

Here is another demo:

http://codepen.io/anon/pen/PPPpOJ

Please note: this also doesn't work when using ngInclude templates.

ThomasBurleson avatar Sep 13 '15 15:09 ThomasBurleson

Hey guys, I fixed this locally with a small little function. They have a hide/close function in the controller, but it's not being exposed. So all I did was add the $mdMenu service to my directive, and access the hide function directly, and i just call my function before I call an individual open. Good hotfix until they patch the $mdOpenMenu function

  $scope.closeOthers = function() {
    $mdMenu.hide(null, { closeAll: true });
  }

christrude avatar Sep 15 '15 02:09 christrude

@christrude Can you please give me a sample layout in codepen. I can't able to reproduce

Jayaraj8905 avatar Sep 18 '15 03:09 Jayaraj8905

Here is an example Codepen that illustrates the reported issue.

After some research I noticed that it happens because md-menu directives use $scope.$emit for triggering the $mdMenuOpen event on which listen all md-menu instances. $emit triggers listeners on all parent scopes including the one on which the event is emitted. That means if all md-menu instances are on the same scope everything is fine but that is not the case with ng-repeat which creates new scope for each child.

saskodh avatar Oct 19 '15 23:10 saskodh

I tried to implement @christrude's answer but I encountered a little problem. If you click menu1->menu2->menu1 again fast enought, it seems that menu1 will not be opened because it will still be hit by the close function.

reylibutan avatar Oct 20 '15 06:10 reylibutan

@reylibutan The problem is that you are too fast. Gonna have to slow down a bit. ;-)

christrude avatar Oct 20 '15 14:10 christrude

@Jayaraj8905 I'll try to get a codePen up for you today

christrude avatar Oct 20 '15 14:10 christrude

+1

johnSowingo avatar Oct 26 '15 17:10 johnSowingo

:+1:

andrew-cunliffe avatar Oct 30 '15 16:10 andrew-cunliffe