Angular Routers
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Agenda
Why Routers?
1
What is Routers?
2
AngularJS Routers
3
Angular Router
4
Demo
6
Diff b/w AngularJS and
Angular Routers
5
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Why we need Routers?
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Why we need Routers?
Navigation Bar
Side Bar Course List
While navigating through a Single Page Application…
Navbar Component
Side Bar Component Course List Component
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Why we need Routers?
Navigation Bar
Side Bar
Course1
Course3
Course4
Course2
If you reload a part (Course List), it only renders that component without fetching the
whole page (i.e. Navigation Bar & Side Bar) from server
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Why we need Routers?
Navigation Bar
Side Bar
Course 2 Description:
… … …
… … …
Similarly, if you select a course, it only renders Course Description instead of reloading
the whole page
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
For a real-time example
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Why we need Routers?
In Gmail inbox while clicking on a mail…
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Why we need Routers?
It only reloads the concerned section, without interfering the rest of the part …
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Without reloading the whole page we can navigate
between components
Thanks to Routers!!!
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
What is Router?
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
What is Router?
Routing is used for navigating among different components in your SPA (Single Page Application)
Component
1
Component
3
Component
6
Component
4
Component
5
Component
2
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
AngularJS Routers
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
AngularJS Routers
NgRoute
UI-Router
UI-Router is a contributed module which is overcome the problems of ngRoute.
Mainly Nested/Complex views.
ngRoute is a angular core module which is good for basic scenarios
• Non official router
• Created by the community
• Used by 99.9% of devs
• More popular than ngRoute
• Mainly used in Nested/Complex views
ANGULARJS
UI-ROUTER
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
UI Route over ng-Route
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
AngularJS Routers
UI-Route over NgRoute
UI-Router allows for nested
views and multiple views
UI-Router allows for you to have
strong-type linking between states
based on state names
You can easily pass information
between states
Change the url in one place will update
every link to that state when you build
your links with ui-sref
States allow you to map and access
different information about different
states
Concept of the decorator in UI-Router,
allows your routes to be dynamically
created based on the URL
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
UI-Router vs NgRoute
Parameters UI-Router NgRoute
Module name (dependent module) ui.router
angular.module('app', ['ui.router']);
ngRoute
angular.module('app', ['ngRoute']);
Router provider $stateProvider
$urlRouterProvider
$routeProvider
Simple Syntax $stateProvider.state('customersState', {
url: '/customers', template: 'My
Customers' })
$routeProvider.when('/customers', {
template: 'My Customers' });
Template view directive ui-view ng-view
Template named view directive ui-view="customers" ---
Link directive ui-sref=""
a ui-sref="customersState"> Customers
</a>
href=""
<a href="#/customers"> Customers
</a>
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
UI-Router vs NgRoute
Parameters UI-Router NgRoute
Getting Params (as a service) $state
(eg) $state.params.id
$stateParams
(eg) $stateParams.id
$route
(eg) $route.current.params.id
$routeParams
(eg) $routeParams.id
Router start event $stateChangeStart $routeChangeStart
Router success event $stateChangeSuccess $routeChangeSuccess
Router error event $stateChangeError $routeChangeError
Router update event --- $routeUpdate
Router not found event $stateNotFound ---
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
UI-Router vs NgRoute
Parameters UI-Router NgRoute
Default View $urlRouterProvider.otherwise('/custom
ers');
$routeProvider.otherwise({redirectTo:
'/customers'});
One view to another view $state.go('customersState'); $location.path( "/customers" );
One view to another view with params $state.go('customersState', {id:'123'}); $location.path( "/customer/123" );
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Angular Router
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Angular Router
• Official Angular Router
• Inspired by the UI-Router
• Complete rewrite as compared to ng-
Route
• Introduction of Route in place of State
ANGULAR
ROUTER
Component
1
Component
3
Component
6
Component
4
Component
5
Component
2
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Router
A router allows us to navigate across components in a Web App
Navbar Component
About Component
Movies Component
Home Component
Home Page View of
the App
It contains the About
section
It shows the list of top 250
movies
Responsible for
navigation among
components
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
A Closer Look at Router
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Router
Path Declaration for different
components
Passing Declared Paths Array to
RouterModule
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
routerLink
Navigation from Movies List Component to Move
Details Component
MoviesComponent MoviesDetailsComponent
[ routerLink ] = “ [ ‘ < path >’ ] ”
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
<router-outlet>
Allows to navigate to a Component
by pasting its path in Browser Address Bar
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
<router-outlet>
Home Page Movies List
Add ‘/movies’ to Browser Address Bar
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Angular Router vs UI-Router
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
UI-Router vs Angular Router
UI ROUTER ANGULAR ROUTER
• A Route in Angular Router
• A Route is the association of a name, a path and
a component
• A State in UI Router
• A State is the association of a name, a url, a
template and a controller
AngularJS Angular
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
UI-Router vs Angular Router
• A Route in Angular Router
• A Route is the association of a name, a path and
a component
• A State in UI Router
• A State is the association of a name, a url, a
template and a controller
UI ROUTER ANGULAR ROUTER
AngularJS Angular
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Nested Views
Navigation Bar
Side Bar
Course1
Course3
Course4
Course2
View inside a view  Course List inside Root Component
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Nested Views
UI ROUTER ANGULAR ROUTER
AngularJS Angular
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Built-in Router Directives
routerLink directive for Angular routerui-sref directive for UI router
UI ROUTER ANGULAR ROUTER
AngularJS Angular
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Built-in Router Directives
routerLink directive for Angular routerui-sref directive for UI router
UI ROUTER ANGULAR ROUTER
AngularJS Angular
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
URL Data
Get the id from /course-details/:id
id of every course
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
URL Data
Get the id from /course-details/:id
RouteParams service for Angular2 Router$stateParams service for UI-Router
id of every course
UI ROUTER ANGULAR ROUTER
AngularJS Angular
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Angular Router Demo
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Demo
Navbar Component
About Component
Course Component
Home Component
Home Page View of
the App
It contains the About
section
It shows the list Edureka
courses
Responsible for navigation
among components
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Demo
Add ‘/course’ to Browser Address Bar
Navigating to Course Detail Component
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Demo
Course Component
(Displays the list of courses)
Navbar Component
Course
Component
About
Component
Course Component
Navbar Component
Course
Component
About
Component
Firebase
Service
Course Details Component
(Displays the details of course selected)
EDUREKA ANGULARJS CERTIFICATION TRAINING www.edureka.co/angular-js
Thank You …
Questions/Queries/Feedback

Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | Edureka

  • 1.
  • 2.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Agenda Why Routers? 1 What is Routers? 2 AngularJS Routers 3 Angular Router 4 Demo 6 Diff b/w AngularJS and Angular Routers 5
  • 3.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Why we need Routers?
  • 4.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Why we need Routers? Navigation Bar Side Bar Course List While navigating through a Single Page Application… Navbar Component Side Bar Component Course List Component
  • 5.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Why we need Routers? Navigation Bar Side Bar Course1 Course3 Course4 Course2 If you reload a part (Course List), it only renders that component without fetching the whole page (i.e. Navigation Bar & Side Bar) from server
  • 6.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Why we need Routers? Navigation Bar Side Bar Course 2 Description: … … … … … … Similarly, if you select a course, it only renders Course Description instead of reloading the whole page
  • 7.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js For a real-time example
  • 8.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Why we need Routers? In Gmail inbox while clicking on a mail…
  • 9.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Why we need Routers? It only reloads the concerned section, without interfering the rest of the part …
  • 10.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Without reloading the whole page we can navigate between components Thanks to Routers!!!
  • 11.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js What is Router?
  • 12.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js What is Router? Routing is used for navigating among different components in your SPA (Single Page Application) Component 1 Component 3 Component 6 Component 4 Component 5 Component 2
  • 13.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js AngularJS Routers
  • 14.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js AngularJS Routers NgRoute UI-Router UI-Router is a contributed module which is overcome the problems of ngRoute. Mainly Nested/Complex views. ngRoute is a angular core module which is good for basic scenarios • Non official router • Created by the community • Used by 99.9% of devs • More popular than ngRoute • Mainly used in Nested/Complex views ANGULARJS UI-ROUTER
  • 15.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js UI Route over ng-Route
  • 16.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js AngularJS Routers UI-Route over NgRoute UI-Router allows for nested views and multiple views UI-Router allows for you to have strong-type linking between states based on state names You can easily pass information between states Change the url in one place will update every link to that state when you build your links with ui-sref States allow you to map and access different information about different states Concept of the decorator in UI-Router, allows your routes to be dynamically created based on the URL
  • 17.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js UI-Router vs NgRoute Parameters UI-Router NgRoute Module name (dependent module) ui.router angular.module('app', ['ui.router']); ngRoute angular.module('app', ['ngRoute']); Router provider $stateProvider $urlRouterProvider $routeProvider Simple Syntax $stateProvider.state('customersState', { url: '/customers', template: 'My Customers' }) $routeProvider.when('/customers', { template: 'My Customers' }); Template view directive ui-view ng-view Template named view directive ui-view="customers" --- Link directive ui-sref="" a ui-sref="customersState"> Customers </a> href="" <a href="#/customers"> Customers </a>
  • 18.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js UI-Router vs NgRoute Parameters UI-Router NgRoute Getting Params (as a service) $state (eg) $state.params.id $stateParams (eg) $stateParams.id $route (eg) $route.current.params.id $routeParams (eg) $routeParams.id Router start event $stateChangeStart $routeChangeStart Router success event $stateChangeSuccess $routeChangeSuccess Router error event $stateChangeError $routeChangeError Router update event --- $routeUpdate Router not found event $stateNotFound ---
  • 19.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js UI-Router vs NgRoute Parameters UI-Router NgRoute Default View $urlRouterProvider.otherwise('/custom ers'); $routeProvider.otherwise({redirectTo: '/customers'}); One view to another view $state.go('customersState'); $location.path( "/customers" ); One view to another view with params $state.go('customersState', {id:'123'}); $location.path( "/customer/123" );
  • 20.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Angular Router
  • 21.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Angular Router • Official Angular Router • Inspired by the UI-Router • Complete rewrite as compared to ng- Route • Introduction of Route in place of State ANGULAR ROUTER Component 1 Component 3 Component 6 Component 4 Component 5 Component 2
  • 22.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Router A router allows us to navigate across components in a Web App Navbar Component About Component Movies Component Home Component Home Page View of the App It contains the About section It shows the list of top 250 movies Responsible for navigation among components
  • 23.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js A Closer Look at Router
  • 24.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Router Path Declaration for different components Passing Declared Paths Array to RouterModule
  • 25.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js routerLink Navigation from Movies List Component to Move Details Component MoviesComponent MoviesDetailsComponent [ routerLink ] = “ [ ‘ < path >’ ] ”
  • 26.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js <router-outlet> Allows to navigate to a Component by pasting its path in Browser Address Bar
  • 27.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js <router-outlet> Home Page Movies List Add ‘/movies’ to Browser Address Bar
  • 28.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Angular Router vs UI-Router
  • 29.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js UI-Router vs Angular Router UI ROUTER ANGULAR ROUTER • A Route in Angular Router • A Route is the association of a name, a path and a component • A State in UI Router • A State is the association of a name, a url, a template and a controller AngularJS Angular
  • 30.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js UI-Router vs Angular Router • A Route in Angular Router • A Route is the association of a name, a path and a component • A State in UI Router • A State is the association of a name, a url, a template and a controller UI ROUTER ANGULAR ROUTER AngularJS Angular
  • 31.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Nested Views Navigation Bar Side Bar Course1 Course3 Course4 Course2 View inside a view  Course List inside Root Component
  • 32.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Nested Views UI ROUTER ANGULAR ROUTER AngularJS Angular
  • 33.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Built-in Router Directives routerLink directive for Angular routerui-sref directive for UI router UI ROUTER ANGULAR ROUTER AngularJS Angular
  • 34.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Built-in Router Directives routerLink directive for Angular routerui-sref directive for UI router UI ROUTER ANGULAR ROUTER AngularJS Angular
  • 35.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js URL Data Get the id from /course-details/:id id of every course
  • 36.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js URL Data Get the id from /course-details/:id RouteParams service for Angular2 Router$stateParams service for UI-Router id of every course UI ROUTER ANGULAR ROUTER AngularJS Angular
  • 37.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Angular Router Demo
  • 38.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Demo Navbar Component About Component Course Component Home Component Home Page View of the App It contains the About section It shows the list Edureka courses Responsible for navigation among components
  • 39.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Demo Add ‘/course’ to Browser Address Bar Navigating to Course Detail Component
  • 40.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Demo Course Component (Displays the list of courses) Navbar Component Course Component About Component Course Component Navbar Component Course Component About Component Firebase Service Course Details Component (Displays the details of course selected)
  • 41.
    EDUREKA ANGULARJS CERTIFICATIONTRAINING www.edureka.co/angular-js Thank You … Questions/Queries/Feedback