Angular ui router multiple named views for all states

后端 未结 2 1174
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 09:35

I want to know if there is any way to write multiple named view for all states, the best example is when i want the nav bar and footer to appear in all routes.



        
相关标签:
2条回答
  • 2020-12-29 10:00

    Yes you can, its actually written in the ui-router's guide on how to manage Multiple Named Views.

    First, you need to define a specific set of named views in an abstract state, including the view where you would put all your content views such as your home.html and put it in a nameless view (empty string).

    As you may have noticed, the demo below shows a root state named app, which is also an abstract state (this means you can't navigate in this state). It has three views, each represents a name that corresponds to the ui-views defined in the index.html.

    Within the nameless view, contains the content.html that has a nameless ui-view that will represent all the child states of the app state. By doing this, you can share the nav.html and footer.html to all your states if you add these states under the app state. An example to this would be the app.home and app.items state. To learn more about this, read the link I've added above.

    DEMO

    Javascript

    $urlRouterProvider.otherwise('/home');
    
    $stateProvider.state('app', {
      abstract: true,
      views: {
       nav: {
         templateUrl: 'nav.html',
         controller: 'NavController as Nav'
       },
    
       '': {
         templateUrl: 'content.html',
         controller: 'ContentController as Content'
       },
    
       footer: {
         templateUrl: 'footer.html',
         controller: 'FooterController as Footer'
       }
    
      }
    })
    
    .state('app.items', {
      url: '/items',
      templateUrl: 'items.html',
      controller: 'ItemsController as Items'
    })
    
    .state('app.home', {
      url: '/home',
      templateUrl: 'home.html',
      controller: 'HomeController as Home'
    });
    

    HTML

    index.html

    <ui-view name="nav"></ui-view>
    <ui-view></ui-view>
    <ui-view name="footer"></ui-view>
    

    content.html

    <hr>
    <ui-view></ui-view>
    <hr>
    
    0 讨论(0)
  • 2020-12-29 10:18

    Depending on the rest of your routes you can probably make use of the abstract state to do this:

    Angular UI Router - Views in an Inherited State might also help point you in the right direction.

    0 讨论(0)
提交回复
热议问题