【发布时间】:2014-11-07 18:55:46
【问题描述】:
需要一些关于将我的 ngRoute 配置迁移到 ui.router 配置的指导。目前我有一个主模板(index.html),它有一个注入所有视图的 ng-view。我目前的 ngRoute 配置如下:
app.config(function ($routeProvider) {
$routeProvider
.when('/login', {
templateUrl: 'app/views/login.html',
controller: 'LoginCtrl'
})
.when('/contact', {
templateUrl: 'app/views/contact.html',
controller: 'ContactCtrl'
})
.when('/notification', {
templateUrl: 'app/views/notification.html',
controller: 'NotificationCtrl'
})
.otherwise({
redirectTo: '/login'
});
我现在想在 index.html 中定义第二个位置,我可以在其中注入一些视图内容——不是嵌套视图,而是另一个 ng-view(或 ui-router 术语中的 ui-view)。原始的 ng-view 部分是默认部分(目前仅用于 /login 和 /contact),而新部分仅用于特定路由(当前仅 '/notification' 但将来可能还有其他)。让我们将新的 ui-view 称为“通知视图”。
我已经阅读了大部分 ui-router 文档,但仍然不确定如何将上述内容迁移到新的 ui.router 配置中。有人可以让我开始或指出一些体面的例子吗?
更新: 好的,这就是我所在的位置。我在 index.html 页面中添加了一些状态和新的 ui 视图。见下文:
<div class="container">
<div id="header"></div>
<div data-ui-view></div>
<div data-ui-view="notification-view"></div>
</div>
我现在的路线是:
app.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/login');
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'app/views/login.html',
controller: 'LoginCtrl'
})
.state('contact', {
url: '/contact',
templateUrl: 'app/views/contact.html',
controller: 'ContactCtrl'
})
.state('notification', {
url: '/notification',
views: {
"notification-view": {
templateUrl: 'app/views/notification.html',
controller: 'NotificationCtrl'
}
}
});
});
这似乎在大多数情况下都可以正常工作。当触发 url /notification 时,应用程序被路由到 NotificationCtrl 并将 ui-view 内容呈现到 notification-view 中。然而,唯一的问题是主(未命名)ui 视图中的 ui 内容丢失了。我希望主 ui 视图中已经呈现的任何内容都保持不变,并且只针对 notification-view。这可能吗?是否必须改为嵌套视图?
【问题讨论】:
-
您希望在“/notification”路由中为未命名的 ui-view 使用哪个模板和控制器?
-
@KaushickGope 未命名的 ui-view 是什么意思?在上面的路由中,我有一个名为“notification-view”的视图:Controller=NotificationCtrl 和 templateUrl=app/views/notification.html
-
未命名的 ui-view 表示您在 'notification-view' "" 之前添加的内容
标签: angularjs migration angular-ui-router ngroute