Doubly-nested views in UI Router

拈花ヽ惹草 提交于 2019-12-24 05:13:07

问题


Playing around with angular-ui and specifically using the ui-router module to do some nested views. I'm having trouble getting a partial to render within a partial:

The nesting is as follows:

index.html
    -main.form.html
    -main.sidebar.html
        -sidebar.slider.html

My current app.js setup is:

$stateProvider
        .state('main', {
            url: '/',
            views: {
                'sidebar': {
                    templateUrl: 'views/partials/main.sidebar.html',
                    views: {
                        'slider': {
                            templateUrl: 'views/partials/sidebar.slider.html'
                        }

                    }

                },
                'form': {
                    templateUrl: 'views/partials/main.form.html',

                },
                'tribute': {
                    templateUrl: 'views/partials/main.tribute.html',
                },
                'submit': {
                    templateUrl: 'views/partials/submit.html',
                }
            }
        })

All other partials load and I can see the ui-view directive loading in the browser, but the actual partial isn't rendered (the div just contains the ui-view="sidebar.slider" literal)

Any thoughts?


回答1:


The view nesting, inside of one state, is done via their relative/absolute view name (not via object nesting)

Let's say, that the templateUrl contains the ui-view="slider", then we have to target that view name absolutely

$stateProvider
   .state('main', {
     url: '/',
     views: {
      'sidebar': {
         templateUrl: 'views/partials/main.sidebar.html',
       },
      // this view definition is on the same level
      // but the absolute name says, that it should be searched
      // inside of the 'main' state
      'slider@main': {
         templateUrl: 'views/partials/sidebar.slider.html'
      },
      ...

the key here is the name of the view 'slider@main', which contains from view name 'slider' delimiter '@' and the state name 'main'. Check these for more details:

  • Combining nested and multiple views for a single state
  • View Names - Relative vs. Absolute Names


来源:https://stackoverflow.com/questions/24638677/doubly-nested-views-in-ui-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!