我计划在我的大型应用程序中使用AngularJS。所以我正在寻找合适的模块来使用。

ngRoute (angular-route.js)和ui-router (angular-ui-router.js)模块之间的区别是什么?

在许多文章中,当使用ngRoute时,route是用$routeProvider配置的。然而,当与ui-router一起使用时,route使用$stateProvider和$urlRouterProvider配置。

我应该使用哪个模块来获得更好的可管理性和可扩展性?


当前回答

ng-View(由AngularJS团队开发)每个页面只能使用一次,而ui-View(第三方模块)每个页面可以使用多次。

ui-View因此是最好的选择。

其他回答

角1.倍

ng-route:

ng-route由angularJS团队开发用于路由。

ng-route:基于url位置的路由。

Ex:

$routeProvider
    .when("/home", {
        templateUrl : "home.html"
    })

ui-route:

ui路由器由第三方模块开发。

Ui-router:基于状态的路由

Ex:

 $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'home.html'
        })

——> ui-router允许嵌套视图

——> ui-router比ng-route更强大

ng-router, ui-router

ngRoute是Angular团队构建的一个模块,提供基本的客户端路由功能。这个模块为路由提供了一个相当强大的基础,并且可以很容易地构建在此基础上,从而提供可靠的路由功能,如本文所示(一定要阅读Ward Bell和作者Ben Nadel之间的评论——他们都是Angular的专业人士)。

Ui-router将焦点从以url为中心的路由转移到应用程序的“状态”上,这些状态可能反映在url中,也可能不反映在url中。

ui-router添加的主要功能是嵌套状态和命名视图。

嵌套状态允许您为应用程序的各个部分分离控制器逻辑。一个非常简单的例子是,应用程序的主要导航在顶部,辅助导航列表在左侧,内容在右侧。如果没有嵌套状态,单个控制器通常必须处理次要导航和内容的显示逻辑。嵌套路由允许分离这些关注点。

Named views are another additional feature of ui-router. With ngRoute, you can only have a single ngView directive on a page, whereas with named views in ui-router you can specify multiple ui-view directives, and then each state is able to affect the template and controller of the names views. A super simple example of this would be to have the main content of your app be the primary view, and then to also have a footer bar that would be a separate ui-view. In this scenario, the footer's controller no longer has to listen for state/route changes.

ngRoute和ui-router的比较可以在这个播客中找到。

让事情变得更复杂的是,请密切关注Angular团队期待为Angular 1.5和2.0版本发布的新的“官方”路由模块。这将取代ngRoute模块。这里是新路由器模块的当前文档——由于实现还没有最终确定,所以在这篇文章中它是相当稀疏的。看这里更多的新闻,当这个模块将实际发布。

ng-View(由AngularJS团队开发)每个页面只能使用一次,而ui-View(第三方模块)每个页面可以使用多次。

ui-View因此是最好的选择。

1- ngRoute是由angular团队开发的,而ui-router是第三方模块。ngRoute基于路由URL实现路由,而ui-router基于应用程序的状态实现路由。3- ui-router提供ng-route提供的所有功能,并加上一些额外的功能,如嵌套状态和多个命名视图。

ngRoute是一个基本的路由库,你可以为任何路由指定一个视图和控制器。

使用ui-router,你可以指定多个视图,包括并行视图和嵌套视图。因此,如果你的应用程序需要(或将来可能需要)任何复杂的路由/视图,那么就使用ui-router。

这是AngularUI路由器最好的入门指南。