material icon indicating copy to clipboard operation
material copied to clipboard

$mdSidenav throws error condition

Open kschwidder opened this issue 11 years ago • 7 comments

In my .run function of the application I have a listener for locationChangeStart as follows:

          $rootScope.$on("$locationChangeStart",function(event, next, current){
                    $mdSidenav('left_panel').close();
                });

On starting the application the left_panel has not been initialized yet and the error "No instance found for handle left_panel" is raised.

Recommendation: If the panel is not initialized $mdSidenav should exit the function.

kschwidder avatar Feb 13 '15 12:02 kschwidder

Does the sidenav have a component id of left_panel? You can use this attribute: md-component-id="left_panel"

marcysutton avatar Feb 13 '15 22:02 marcysutton

It does. The run block is running first and therefore the $mdSidenav object left-panel has not been initialized as the html is loaded after the location has changed.

kschwidder avatar Feb 15 '15 20:02 kschwidder

References #1311

robertmesserle avatar Feb 19 '15 18:02 robertmesserle

Closing as a duplicate of #1311.

rschmukler avatar Apr 05 '15 21:04 rschmukler

@kschwidder Have you solved the problem? I still have the problem after applying #1311. Just like you case, in my .run function, I use

$rootScope.$on("$locationChangeStart", function(event, next, current){
    $mdSidenav("left").then(function(left){ left.close();});
 });

and md-sidenav directive in a template file(loaded by ng-include)

<md-sidenav class="md-sidenav-left md-whiteframe-z2" md-component-id="left" md-is-open="isSidenavOn" md-swipe-left="toggleSidenav('close')">
...
</md-sidenav>

hankchiutw avatar Jun 23 '15 14:06 hankchiutw

I am having the same issue using UI-Router. I use $viewContentLoaded in my my run function to close a sidebar after navigation. The first time the event fires, there are no instances registered in the function ComponentRegistry of the angular-material.js of the module material.core.componentRegistry. Second time it is triggers (gets triggered 3 times on page reload in my app) it does have instances registered and the lookup goes fine.

I must tell you that I am having a 2 view system (code in Coffee):

    $stateProvider

      # abstract views / layout
      .state 'private',
        url: ''
        abstract: true
        views:
          'app':
            templateUrl: 'client/views/layouts/privateLayout.html'

      .state 'public',
        url: ''
        abstract: true
        views:
          'app':
            templateUrl: 'client/views/layouts/publicLayout.html'

      # start private routes
      .state 'private.home',
        url: '/'
        views:
          "container@private":
            templateUrl: 'client/views/home/home.html'

So when the route private.home is loaded, the event viewContentLoaded fires 3 times. First to load the master index file I guess and then when it renders the privateGlobalLayout (which has the sidebar) and then the inner view which. When loading the index file

head
  meta(name="viewport" content="width=device-width, initial-scale=1")
  base(href="/")

body(layout="column")
  div(ui-view="app" layout="column" flex)

which doesn' thave any instance registered, it throws an error.

The private Global Layout

div(layout="column" flex)
  div(ng-controller="AppCtrl" layout="row" flex)
      md-sidenav.md-sidenav-left.md-whiteframe-z2(md-component-id='left' md-is-locked-open="$mdMedia('gt-md')" md-swipe-left="close('left')")
        md-toolbar(layout="row" layout-align="center center")
etc....

So maybe my code is wrong, because it tries to get the sidebar instance when there is no registration of it on the first event fire. The second fire will find an instance registered. So I might have to look in my router to prevent it to try to look up the side bar on the first fire

mattiLeBlanc avatar Aug 29 '15 05:08 mattiLeBlanc

@kwela Check solution here: https://github.com/SebastianMetzger/angular-material-inbox/issues/1

It works for now, using promise on mdComponentRegistry!

mattiLeBlanc avatar Aug 29 '15 05:08 mattiLeBlanc