Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
104
rated 0 times [  105] [ 1]  / answers: 1 / hits: 113438  / 10 Years ago, thu, december 25, 2014, 12:00:00

Please explain the difference between $routeProvider and $stateProvider in AngularJS.



Which is best practice?


More From » angularjs

 Answers
43

Both do the same work as they are used for routing purposes in SPA(Single Page Application).



1. Angular Routing - per $routeProvider docs




URLs to controllers and views (HTML partials). It watches
$location.url() and tries to map the path to an existing route
definition.




HTML



<div ng-view></div>


Above tag will render the template from the $routeProvider.when() condition which you had mentioned in .config (configuration phase) of angular



Limitations:-




  • The page can only contain single ng-view on page

  • If your SPA has multiple small components on the page that you wanted to render based on some conditions, $routeProvider fails. (to achieve that, we need to use directives like ng-include, ng-switch, ng-if, ng-show, which looks bad to have them in SPA)

  • You can not relate between two routes like parent and child relationship.

  • You cannot show and hide a part of the view based on url pattern.






2. ui-router - per $stateProvider docs




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




Multiple & Named Views



Another great feature is the ability to have multiple ui-views in a template.



While multiple parallel views are a powerful feature, you'll often be able to manage your interfaces more effectively by nesting your views, and pairing those views with nested states.



HTML



<div ui-view>
<div ui-view='header'></div>
<div ui-view='content'></div>
<div ui-view='footer'></div>
</div>


The majority of ui-router's power is it can manage nested state & views.



Pros




  • You can have multiple ui-view on single page

  • Various views can be nested in each other and maintained by defining state in routing phase.

  • We can have child & parent relationship here, simply like inheritance in state, also you could define sibling states.

  • You could change the ui-view=some of state just by using absolute routing using @ with state name.

  • Another way you could do relative routing is by using only @ to change ui-view=some. This will replace the ui-view rather than checking if it is nested or not.

  • Here you could use ui-sref to create a href URL dynamically on the basis of URL mentioned in a state, also you could give a state params in the json format.



For more Information Angular ui-router



For better flexibility with various nested view with states, I'd prefer you to go for ui-router


[#68386] Tuesday, December 23, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devlin

Total Points: 474
Total Questions: 113
Total Answers: 100

Location: Sweden
Member since Fri, Apr 16, 2021
3 Years ago
devlin questions
Tue, Apr 27, 21, 00:00, 3 Years ago
Sat, Oct 31, 20, 00:00, 4 Years ago
Fri, Aug 28, 20, 00:00, 4 Years ago
;