Please explain the difference between $routeProvider
and $stateProvider
in AngularJS.
Which is best practice?
This question is related to
javascript
angularjs
angular-ui-router
angularjs-routing
$route: This is used for deep-linking URLs to controllers and views (HTML partials) and watches $location.url() in order to map the path from an existing definition of route.
When we use ngRoute, the route is configured with $routeProvider and when we use ui-router, the route is configured with $stateProvider and $urlRouterProvider.
<div ng-view></div>
$routeProvider
.when('/contact/', {
templateUrl: 'app/views/core/contact/contact.html',
controller: 'ContactCtrl'
});
<div ui-view>
<div ui-view='abc'></div>
<div ui-view='abc'></div>
</div>
$stateProvider
.state("contact", {
url: "/contact/",
templateUrl: '/app/Aisel/Contact/views/contact.html',
controller: 'ContactCtrl'
});
Angular's own ng-Router takes URLs
into consideration while routing, UI-Router takes states
in addition to URLs.
States are bound to named, nested and parallel views, allowing you to powerfully manage your application's interface.
While in ng-router, you have to be very careful about URLs when providing links via <a href="">
tag, in UI-Router you have to only keep state
in mind. You provide links like <a ui-sref="">
. Note that even if you use <a href="">
in UI-Router, just like you would do in ng-router, it will still work.
So, even if you decide to change your URL some day, your state
will remain same and you need to change URL only at .config
.
While ngRouter can be used to make simple apps, UI-Router makes development much easier for complex apps. Here its wiki.
Source: Stackoverflow.com