22

I have a configuration like below:

...
.state('profiles', {
    abstract: true
    , url : '/profiles'
    , resolve: {...}    
    , views: {
      mainModule: {
        templateUrl : '/partials/home.html'
        , controller : 'HomeCtrl'
      }
      , leftSidePaneModule: {
          templateUrl : '/partials/leftSidePane.html'
          , controller : 'LeftSidePaneCtrl'
      }
    }
  })
...

Main Jade File

...
.col-xs-4.col-md-4.chat_block(ui-view='leftSidePaneModule', autoscroll="false")  
...

So, leftSidePaneModule module gets populated in to ui-view='leftSidePaneModule' placeholder. But the problem is, I have 2 more named views inside leftSidePaneModule template. leftSidePaneModule template looks like this:

leftSidePaneModule.html

<div>
  <div ui-view="leftWidgetOne"></div>
  <div ui-view="leftWidgetTwo"></div>
</div>

How do i make modules leftWidgetOne & leftWidgetOne gets populated in to above placeholder?

I tried,

...
.state('profiles', {
    abstract: true
    , url : '/profiles'
    , resolve: {...}    
    , views: {
      mainModule: {
        templateUrl : '/partials/home.html'
        , controller : 'HomeCtrl'
      }
      , leftSidePaneModule: {
          templateUrl : '/partials/leftSidePane.html'
          , controller : 'LeftSidePaneCtrl'
      }
      , 'leftWidgetOne@leftSidePaneModule' : {...}
      , 'leftWidgetTwo@leftSidePaneModule' : {...}
    }
  })
...

Attempt: 2

...
.state('profiles', {
    abstract: true
    , url : '/profiles'
    , resolve: {...}    
    , views: {
      mainModule: {
        templateUrl : '/partials/home.html'
        , controller : 'HomeCtrl'
      }
      , leftSidePaneModule: {
          templateUrl : '/partials/leftSidePane.html'
          , controller : 'LeftSidePaneCtrl'
          , views: {
             'leftWidgetOne' : {...}
             , 'leftWidgetTwo' : {...}
          }
      }
    }
  })
...

Both are not working.

How do you do it?

Thanks!

  • This might help you along: http://stackoverflow.com/questions/12863663/angularjs-complex-nesting-of-partials-and-templates – Iansen May 11 '14 at 08:42

1 Answers1

65

The solution could be found here: View Names - Relative vs. Absolute Names. A cite:

Behind the scenes, every view gets assigned an absolute name that follows a scheme of viewname@statename, where viewname is the name used in the view directive and state name is the state's absolute name, e.g. contact.item
...
(note: in our case it must be 'profiles').

The point is, that we can use the full (absolute) name of the view, being part of the current state definition:

$stateProvider
    .state('profiles', {
        url: '/profiles',
        views: {
            mainModule: {
                template: '<div>' +
                          '  <h1>Main</h1>' +
                          '  <div ui-view="leftSidePaneModule"></div>' +
                          '</div>',
            },
            // here we do target the view just added above, as a 'mainModule'
            // as <div ui-view="leftSidePaneModule">
            'leftSidePaneModule@profiles': {
                template: '<div>' + 
                            '  <div ui-view="leftWidgetOne"></div>' + 
                            '  <div ui-view="leftWidgetTwo"></div>' +
                            '</div>',
            },
            // and here we do target the sub view
            // but still part of the state 'profiles' defined in the above
            // view defintion 'leftSidePaneModule@profiles'
            'leftWidgetOne@profiles': {
                template: '<h2>One</2>',
            },
            'leftWidgetTwo@profiles': {
                template: '<h2>Two</2>',
            },
        }
    });

There is also plunker showing the above code in action

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • 1
    Seriously, THANK YOU for explaining this. I can't tell you how long I spent trying to figure this out. – subvertallchris Oct 16 '14 at 19:28
  • 1
    this needs to be upvoted so many more times than it is. I was searching forever to find this. – Jon Mattingly Feb 01 '15 at 03:01
  • 1
    I also searched forever for this, this was extremely helpful, thanks a lot! – Stefan Konno Mar 02 '15 at 08:54
  • 1
    thanks a lot for this, I couldn't make it work, I tried several hours without success – user1336321 Jun 22 '15 at 19:37
  • Funny that the docs state something similar but this made more sense to me. Thanks for explaining this. – VeldMuijz Sep 15 '15 at 12:51
  • Can we use profiles as abstract state ? I need to create nested stated with this like profiles.xyz – shruti Nov 29 '15 at 12:07
  • Hi nice answer! Quick question, how would you load another state into one of those named views? http://stackoverflow.com/questions/42956873/angularjs-ui-router-how-to-load-a-state-view-into-another-nested-view-of-a-top-l – Leon Gaban Mar 22 '17 at 16:24