I'm planning to use AngularJS in my big applications. So I'm in the process to find out the right modules to use.
What is the difference between ngRoute (angular-route.js) and ui-router (angular-ui-router.js) modules?
In many articles when ngRoute is used, route is configured with $routeProvider. However, when used with ui-router, route is configured with $stateProvider and $urlRouterProvider.
Which module should I use for better manageability and extensibility?
ui-router is a 3rd-party module and is very powerful. It supports everything the normal ngRoute can do as well as many extra functions.
Here are some common reason ui-router is chosen over 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
onrun
.In essence, ui-router is ngRouter with more features, under the sheets it is quite different. These additional features are very useful for larger applications.
More Information:
Generally ui-router works on a state mechanism... It can be understood with an easy example:
Let's say we have a big application of a music library (like ..gaana or saavan or any other). And at the bottom of the page, you have a music player which is shared across all the state of the page.
Now let's say you just click on some songs to play. In this case, only that music player state should change instead of reloading the full page. That can be easily handled by ui-router.
While in ngRoute we just attach the view and the controller.
ui router make your life easier! You can add it to you AngularJS application via injecting it into your applications...
ng-route
comes as part of the core AngularJS, so it's simpler and gives you fewer options...Look at here to understand ng-route better: https://docs.angularjs.org/api/ngRoute
Also when using it, don't forget to use: ngView ..
ng-ui-router is different but:
https://github.com/angular-ui/ui-router but gives you more options....