Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am new to AngularJS. I find Angular quite interesting and planning to use angular in my big apps. So I am 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.

This creates a bit of confusion for me. Which module should I use for better manageability and extensibility?

share|improve this question
9  
not to mention angular's new router in 1.4+ and 2.0 – Zach Lysobey May 12 '15 at 21:55
1  
Hue hue. 666 upvotes }:-> – Hubert Grzeskowiak Aug 3 at 20:31

13 Answers 13

up vote 888 down vote accepted

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 on run.

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:

share|improve this answer
111  
Overall this is the best explanation, but for one key point: "Overall, ui-router is ngRouter with more features". It's much more fundamental than that: ngRoute merely allows you to assign controllers and templates to URL routes, whereas the fundamental abstraction in ui.router is states, which is a more powerful concept. – Nate Abele Jan 11 '14 at 4:03
2  
@Xvegas this all runs on the client and has nothing to do with the server. But yes, in that aspect the two are the same and would be the same setup. – TheSharpieOne Jul 15 '15 at 16:14
11  
It might be relevant to some to point out difference in filesize in this answer. As of right now ngRoute: 35.9kB / 4.4kB / 2.5kB and ui-router: 162.9kB / 30.4kB / 11.6kB (unminified / minified / gzipped). – Alex Ross Jul 21 '15 at 5:04
24  
Are we seriously worried about 162kb in 2015? – Catfish Dec 9 '15 at 19:46
11  
Why not @Catfish ? There are many places in the world with bad internet connections, worry about it is very important! – Bruno Casali Apr 28 at 18:48

ngRoute is a module developed by the Angular.js team which was earlier part of the Angular core.

ui-router is a framework which was made outside the Angular.js project to improve and enhance routing capabalities.

From the ui-router documentation:

AngularUI Router is a routing framework for AngularJS, which allows you to organize the parts of your interface into a state machine. Unlike the $route service in Angular core, which is organized around URL routes, UI-Router is organized around states, which may optionally have routes, as well as other behavior, attached.

States are bound to named, nested and parallel views, allowing you to powerfully manage your application's interface.

Neither of them is better, you will have to chose the most appropriate for your project.

However, if you plan to have complex views in your application and you would like to deal with the "$state" notion. I recommend you to chose ui-router.

Hope that help !

share|improve this answer

ngRoute is a angular core module which is good for basic scenarios. I believe that they will add more powerful features in upcoming releases.

URL: https://docs.angularjs.org/api/ngRoute

Ui-router is a contributed module which is overcome the problems of ngRoute. Mainly Nested/Complex views.

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

Some of the difference between ui-router and ngRoute

http://www.amasik.com/angularjs-ngroute-vs-ui-router/

enter image description here

share|improve this answer

ngRoute is part of the core AngularJS framework.

ui-router is a community library that has been created to attempt to improve upon the default routing capabilities.

here is a good article about configuring/setting up ui-router:

http://www.ng-newsletter.com/posts/angular-ui-router.html

share|improve this answer

If you want to make use of nested views functionality implemented within ngRoute paradigm, try angular-route-segment - it aims to extend ngRoute rather than to replace it.

share|improve this answer
13  
I don't see the relevancy of your answer. The author asked specifically about the differences between angular-route and angular-ui-router. Also, a disclaimer that you are the creator would be useful when promoting your own libraries. – vially Jun 2 '14 at 17:53
19  
The relevancy is simple: angular-route + angular-route-segment = angular-ui-router. So, the difference is: angular-ui-router - angular-route = angular-route-segment :) – artch Jun 3 '14 at 6:30

Why You Should Use UI-Router:

Multiple Views: Most applications can be broken up into regions. At a minimum, applications usually have a header, a main content area, and a footer.

Commonly, applications may have an additional sidebar on the left or right side of the page as shown below. enter image description here

In most use cases, all of these regions (views) are shown on the page at the same time. With the built-in AngularJS router, ngRoute, only one view (ng-view) is allowed per page. This limitation causes people to use includes (ng-include) or other workarounds to create a layout or master page for their application. UI-Router supports multiple views and each can have it’s own corresponding Controller so that each of these regions can be encapsulated and reused throughout the application if needed.

Nested Views: The common example of a nested view in applications is a master/detail or, more specifically, a list/detail page. Many applications show a list of items then when you click on an item you see the detail for that item. Taking this example further, you might then click an edit link when viewing the item’s details that takes you to an editable form for the item (see the diagram below to visualize). enter image description here

This scenario is easily achieved with the built-in AngularJS router, ngRoute, if the list and detail are on separate pages (or views as they are called in AngularJS). However, if you want the list to remain on the page while you show the detail to the right or below the list this becomes more challenging.

To be clear, this requirement can be achieved with ngRoute by sharing a single view with two controllers: one for the list and one for the detail and hiding and showing the detail as needed.

The result is not ideal because we would like the list and detail to each have their own controller and view with only one responsibility (showing a list or showing item details). By encapsulating these user interface areas in their own view we can have a more composable UI that allows us to bring the pieces together or break them apart as needed to meet requirements.

Nested views enable us to not only to bring these views together at the same time, but also to nest a view inside another view.

share|improve this answer

Generally ui-router work on state mechanism... It can be understand with an easy example... Lets say we have a big application of music library ( like ..gaana or saavan or any other) And at the bottom of the page you have 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 full page. That can be easily handled by ui-router.

While in ngRoute we just attach the view and the controller.

share|improve this answer
2  
this could be done and must be done using services and factories. The use of this package is lack of understanding angular routes, states and patterns. States are handled by the url, which is correct if you want to share the app in a specefic state, moreover you can have multiple controllers in the same view that react on a service data update or url param. ui router mess things up and destroy angular pattern completely. – Pablo Ezequiel Leone Feb 10 at 17:31
    
I completely agree. Still can't find an explanation where it is necessary to use this state machine – MegaRacer Jul 10 at 13:54

ngRoute is a module built by the Angular team that provides basic client-side routing functionality. This module provides a fairly powerful base for routing, and can be built upon pretty easily to give solid routing functionality, as exemplified in this blog post (be sure to read the comment trail between Ward Bell and Ben Nadel, the author - they are a couple of Angular pros)

ui-router shifts the focus from url-centric routes to application "states", which may or may not be reflected in the url.

The primary features added by ui-router are nested states and named views.

Nested states allow you to separate controller logic for the various pieces of the application. A very simple example of this would be an app with primary navigation across the top, a secondary navigation list along the left, and content on the right. Without nested states, a single controller would typically have to handle the display logic for the secondary navigation as well as the content. Nested routing allows you to separate these concerns.

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.

A good comparison of ngRoute and ui-router can be found on this podcast episode.

Just to make things more confusing, keep an eye on the new "official" routing module that the Angular team is expecting to release for versions 1.5 and 2.0 of Angular. This will be replacing the ngRoute module. Here is the current documentation for the new Router module - it is fairly sparse as of this posting since the implementation has not yet been finalized. Watch here for more news on when this module will actually be released.

share|improve this answer

ngRoute is basic routing library, where you can specify just one view and controller for any route.

With ui-router, you can specify multiple views, both parallel and nested. So if you app requires (or may require in future) any kind of complex routing/views, then go ahead with ui-router.

This is best getting started guide for AngularUI Router.

share|improve this answer

AngularUI Router is a routing framework for AngularJS, which allows you to organize the parts of your interface into a state machine. Unlike the $route service in the Angular ngRoute module, which is organized around URL routes, UI-Router is organized around states, which may optionally have routes, as well as other behavior, attached.

https://github.com/angular-ui/ui-router

share|improve this answer

ui router make ur life easier! you can add it to you Angular app via injecting it into your app..

ng-route comes as part of the core Angular, so it's simpler and give u less 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....

share|improve this answer

Basic thing you have to know: ng-router uses $locarion.go and ui-router uses $state.go

Rest us all features.

share|improve this answer

ng-route:

ng-route is developed by the angularJS Team for routing.

ng-route: url (Location) based routing.

Ex:

$routeProvider
    .when("/", {
        templateUrl : "main.htm"
    })

ui-route:

ui-router is develoepd by 3rd party module.

ui-router : state based routing

Ex:

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

--> ui-router allows for nested views

--> ui-router more powerful than ng-route

ng-router, ui-router

share|improve this answer

protected by Community Jan 5 at 6:06

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.