【问题标题】:Migrate from ngRoute to ui-router从 ngRoute 迁移到 ui-router
【发布时间】: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


【解决方案1】:

使用 ui.router 时,您应该考虑状态而不是路由。所以不是$routeProvider,而是注入$stateProvider,计划各种状态并从那里开始工作。因此,从上面的示例中,我们将其转换为:

app.config(function ($stateProvider,$urlRouterProvider) {

    $urlRouterProvider.otherwise('/');

    $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',
        templateUrl: 'app/views/notification.html',
        controller: 'NotificationCtrl'
    });
}

向uirouter添加“子视图”有很多方法,一种方法是添加子状态。

$stateProvider
        .state('login', {
            url:'/login',
            templateUrl: 'app/views/login.html',
            controller: 'LoginCtrl'
        })
          .state('login.error', {
            url:'/login',
            templateUrl: 'app/views/login-error-subview.html',
            controller: 'LoginErrorCtrl'
          })

同样$stateProvider 不提供默认状态处理程序,您还需要注入$urlRouterProvider。这是一个还附带 ui-router 的提供程序,其任务是监视 $location 的更改。

与 ui-router 相比,在您开始使用子状态和堆叠状态之前,您不会看到与内置路由提供程序和易用性的巨大差异。

在上面的示例中,ui.router 不知道在 ui-view 中使用什么模板,因此将其留空。你可以给它一个模板,从而变成:

...
.state('notification', {
      url: '/notification',
      views: {
        '':{
            templateUrl: 'app/views/notification-main.html',
            controller: ''              
        }
        'notification-view': {
            templateUrl: 'app/views/notification.html',
            controller: 'NotificationCtrl'              
        }
      }
...

但据我所知,您希望 logincontact 在其中包含通知。因此,理想情况下,您应该为每个子状态创建一个 通知,因为现在可以为子状态声明通配符或多个父状态。希望当 v1.0 发布时,已经可以支持这个用例了。

以下是文档中的链接,可帮助您快速了解:

https://github.com/angular-ui/ui-router/wiki/URL-Routing

https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views

【讨论】:

  • 谢谢你,我会试一试的。但是,查看代码,我看不出哪里可以将新的 ui 视图(即通知视图)定位于默认视图。
  • @user1491636 抱歉编辑应该说.state 而不是.when
  • 如果我之前的所有路由都来自使用 $location.path() 怎么办?以上方法还能用吗?以及如何定位特定的 ui 视图?
  • @user1491636 定位 ui 视图?
  • 在我原来的帖子中,我说过我希望将一些 UI 内容注入到主 ui-view(旧 ng-view)中,并将其他 UI 内容注入到第二个 ui-视图(我称之为通知视图)。我的理解是多视图是ui-router的主要功能之一。因此,对于状态“通知”,我希望该内容仅在我的新 UI 视图“通知视图”中呈现。这可能吗?
猜你喜欢
  • 1970-01-01
  • 2017-06-14
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多