Angular UI-Router多视图

TJF*_*TJF 43 angularjs angular-ui-router

我正在使用角度UI-Router.我的路由配置中有以下内容

.config(function config($stateProvider) {
  $stateProvider.state('newsFeedView', {
      url: '/newsFeed',
      controller: 'newsFeedController',
      templateUrl: '../src/app/bulletinBoard/views/newsFeed.part.html',
      data: {
        pageTitle: 'News Feed'
      }
    })
    .state('tradeFeedView', {
      url: '/tradeFeed',
      controller: 'tradeFeedController',
      templateUrl: '../src/app/bulletinBoard/views/tradeFeed.part.html',
      data: {
        pageTitle: 'Trade Feed'
      }
    })
    .state('bulletinBoard', {
      url: '/bulletinBoard',
      views: {
        'tradeFeed': {
          url: "",
          controller: 'tradeFeedController',
          templateUrl: '../src/app/bulletinBoard/views/tradeFeed.part.html'
        },
        'newsFeed': {
          url: "",
          controller: 'newsFeedController',
          templateUrl: '../src/app/bulletinBoard/views/newsFeed.part.html'
        }
      },
      templateUrl: '../src/app/bulletinBoard/views/bulletinBoard.part.html'
    });
})
Run Code Online (Sandbox Code Playgroud)

在我的索引页面中,我只使用以下方法调用视图:

<div class="container" ui-view></div>
Run Code Online (Sandbox Code Playgroud)

在我的bulletinBoard.html中,我希望有一个嵌套视图:

<div ui-view="tradeFeed"></div>
<div ui-view="newsFeed"></div>
Run Code Online (Sandbox Code Playgroud)

对于/ newsFeed页面和/ tradeFeed页面,这非常有效,但对于公告板,我无法在页面上看到任何内容.我哪里错了?

小智 106

我发现官方GitHub wiki上的例子非常不直观.这是一个更好的:

https://scotch.io/tutorials/angular-routing-using-ui-router

例如:

...

.state('bulletinBoard', {
    url: '/bulletinBoard',
    views: {

        // the main template will be placed here (relatively named)
        '': { templateUrl: '../src/app/bulletinBoard/views/bulletinBoard.part.html' },

        // the child views will be defined here (absolutely named)
        'tradeFeed@bulletinBoard': { template: ..... },

        // another child view
        'newsFeed@bulletinBoard': { 
            templateUrl: ......
        }
    }

});
Run Code Online (Sandbox Code Playgroud)

每个视图属性的语法是viewName@stateName.

  • 教程链接正是我想要的.谢谢! (6认同)
  • 比https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views更好的例子.这简化了我的配置,从抽象状态w/child,这似乎是github wiki建议的.非常感谢! (2认同)

squ*_*dbe 9

使用该对象时,将忽略该.state()方法.有关更多信息,请参阅ui-router wiki:https: //github.com/angular-ui/ui-router/wiki/Multiple-Named-Views#user-content-views-override-states-template-propertiestemplateUrlviews