I have tried to consolidate the difference between $routeprovider Vs $stateprovider.
Both the module does the same work as they are used for routing purposes in SPA. Though we have to decide which one fulfilling our requirement best.
S.No | $routeprovider | $stateprovider |
1 | It uses the AngularJS native module called ngRoute | It uses the third party module called ui-router to improve and enhance routing capabilities |
2 | ng-view can be used only once per page e.g. <div ng-view></div> | ui-View can be used multiple times per page e.g. <div ui-view> <div ui-view='header'></div> <div ui-view='content'></div> <div ui-view='footer'></div> </div> |
3 | It renders the template from the $routeProvider.when() | It renders the template from the $stateProvider. state () |
4 | ngRoute implements routing based on the route URL | ui-router implements routing based on the state of the application |
5 | ng-router uses $location.path() | ui-router uses $state.go() |
6 | ‘ngRoute’ takes care of urls | ‘ui-router’ takes cares of ‘states’ |
7 | It passes information between states with the help of ‘$stateParams’. | |
8 | In ‘ngRoute’ you link directive like below : <a href="#/home"> Home </a> | In ‘ui-router’ link directive are generally written as: <a ui-sref="homeState"> Customers </a> |
9 | Router provider: $routeProvider | Router provider : $stateProvider $urlRouterProvider |
10 | Syntax: $routeProvider.when('/customers', { template: 'My Home' }); | Syntax: $stateProvider.state(homeState, { url: '/home, template: 'My Home' }) |
11 | No template named view directive | Template named view directive: ui-view="home" |
12 | Getting Params: $route (eg) $route.current.params.id $routeParams (eg) $routeParams.id | Getting Params: $state (eg) $state.params.id $staetParams (eg) $stateParams.id |
13 | Router start event: $routeChangeStart | Router start event: $stateChangeStart |
14 | Router success event: $routeChangeSuccess | Router success event: $stateChangeSuccess |
15 | Router error event: $routeChangeError | Router error event: $stateChangeError |
16 | Router update event: $routeUpdate | Router update event: -- |
17 | Router not found event: -- | Router not found event: $stateNotFound |
18 | Default View: $routeProvider.otherwise({redirectTo: '/home'}); | Default View: $urlRouterProvider.otherwise('/ home '); |
19 | One view to another view: $location.path( "/home" ); | One view to another view: $state.go('home'); |
20 | One view to another view with params: $location.path( "/home/123" ); | One view to another view with params: $state.go('homeState', {id:'123'}); |
ui-router does everything that the ng-route provides plus some additional features like nested states and multiple named views. This is very helpful in managing large projects. But if your project is small then ng-route will be good.
No comments:
Post a Comment