我计划在我的大型应用程序中使用AngularJS。所以我正在寻找合适的模块来使用。
ngRoute (angular-route.js)和ui-router (angular-ui-router.js)模块之间的区别是什么?
在许多文章中,当使用ngRoute时,route是用$routeProvider配置的。然而,当与ui-router一起使用时,route使用$stateProvider和$urlRouterProvider配置。
我应该使用哪个模块来获得更好的可管理性和可扩展性?
我计划在我的大型应用程序中使用AngularJS。所以我正在寻找合适的模块来使用。
ngRoute (angular-route.js)和ui-router (angular-ui-router.js)模块之间的区别是什么?
在许多文章中,当使用ngRoute时,route是用$routeProvider配置的。然而,当与ui-router一起使用时,route使用$stateProvider和$urlRouterProvider配置。
我应该使用哪个模块来获得更好的可管理性和可扩展性?
当前回答
通常ui-router工作在一个状态机制…可以用一个简单的例子来理解:
假设我们有一个大的音乐库应用程序(比如..Gaana或saavan或任何其他)。在页面的底部,你有一个音乐播放器在页面的所有状态下共享。
现在让我们假设你只是点击一些歌曲来播放。在这种情况下,只有音乐播放器的状态应该改变,而不是重新加载整个页面。这可以通过ui-router轻松处理。
而在ngRoute中,我们只是附加视图和控制器。
其他回答
你必须知道的基本事情:ng-router使用$location.path(), ui-router使用$state.go
让我们休息一下。
如果你想使用ngRoute范例中实现的嵌套视图功能,可以尝试angular-route-segment -它旨在扩展ngRoute,而不是取代它。
ngRoute是一个基本的路由库,你可以为任何路由指定一个视图和控制器。
使用ui-router,你可以指定多个视图,包括并行视图和嵌套视图。因此,如果你的应用程序需要(或将来可能需要)任何复杂的路由/视图,那么就使用ui-router。
这是AngularUI路由器最好的入门指南。
1- ngRoute是由angular团队开发的,而ui-router是第三方模块。ngRoute基于路由URL实现路由,而ui-router基于应用程序的状态实现路由。3- ui-router提供ng-route提供的所有功能,并加上一些额外的功能,如嵌套状态和多个命名视图。
Ui-router是一个第三方模块,功能非常强大。它支持普通ngRoute所能做的一切,以及许多额外的函数。
下面是ui-router选择ngRoute的一些常见原因:
ui-router allows for nested views and multiple named views. This is very useful with larger app where you may have pages that inherit from other sections. ui-router allows for you to have strong-type linking between states based on state names. Change the url in one place will update every link to that state when you build your links with ui-sref. Very useful for larger projects where URLs might change. There is also the concept of the decorator which could be used to allow your routes to be dynamically created based on the URL that is trying to be accessed. This could mean that you will not need to specify all of your routes before hand. states allow you to map and access different information about different states and you can easily pass information between states via $stateParams. You can easily determine if you are in a state or parent of a state to adjust UI element (highlighting the navigation of the current state) within your templates via $state provided by ui-router which you can expose via setting it in $rootScope on run.
从本质上讲,ui-router是一个具有更多功能的ngRouter,在表下是完全不同的。这些附加特性对于较大的应用程序非常有用。
更多信息:
Github: https://github.com/angular-ui/ui-router 文档: API参考:http://angular-ui.github.io/ui-router/site/#/api 导游:https://github.com/angular-ui/ui-router/wiki 常见问题:https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions 示例应用程序:http://angular-ui.github.io/ui-router/sample/#/