SlideShare a Scribd company logo
1 of 33
Download to read offline
Software IndustrySultan Ahmed Sagor
Angular 7
A framework for Presentation Layer
Software IndustrySultan Ahmed Sagor
Software IndustrySultan Ahmed Sagor
Angular Routing
Software IndustrySultan Ahmed Sagor
Angular Tutorial: Road Covered So Far
Software IndustrySultan Ahmed Sagor
What is Angular Routing ?
Now we will discuss angular routing
Software IndustrySultan Ahmed Sagor
What are Angular Router?
❖ Angular Router is vital part of any application.
❖ Loads the corresponding components with respect to the requested route.
❖ Allows us to control different routes, data and the components that render the data.
Software IndustrySultan Ahmed Sagor
Demo
Now we will understand Router through Demo
Software IndustrySultan Ahmed Sagor
Highlights of Demo
❖ In this demo, we will do the following tasks:
❖ A page with 3 routes (Home, About, User)
❖ We should be able to navigate to the page assigned to each route.
❖ On our “User” page, we should have a sub-route that navigates to a user page; all in the main
“User” page.
❖ Also, if we don’t provide any url route, it should navigate to the “homepage” automatically.
❖ And if we type in a garbage url route (a url that doesn’t exist), it should also navigate to the
homepage as well or a 404 page.
Software IndustrySultan Ahmed Sagor
Component Generation
Now we will create 3 components
Software IndustrySultan Ahmed Sagor
Component Generation
❖ Let us generate the components home, about, userList & user as the following syntax
❖ ng generate component home
❖ ng generate component about
❖ ng generate component userList
❖ ng generate component user
Software IndustrySultan Ahmed Sagor
Component Generation
Software IndustrySultan Ahmed Sagor
Routes Setup
Now we will setup routes
Software IndustrySultan Ahmed Sagor
Routes Setup
❖ Let’s set up the routes. The routes will be set up in app.module.ts.
❖ Inside this file, all the 4 components we created earlier have been imported.
// app.module.ts
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { UserListComponent } from './user-list/user-list.component';
import { UserComponent } from './user/user.component';
Software IndustrySultan Ahmed Sagor
Routes Setup
❖ The 4 components have also been declared in the @NgModule declaration in our app.module.ts file.
❖ basically, this helps us set the initial component property.
// app.module.ts
@NgModule({
declarations: [
AppComponent,
HomeComponent,
AboutComponent,
UserListComponent,
UserComponent
],
Software IndustrySultan Ahmed Sagor
Routes Setup
❖ We will need to import the router from the angular route library.
❖ Now that we have imported the router, the routes have to be configured by creating an object (const
appRoutes..).
❖ This object will have all the information about the url path, components and redirect path for the
routes.
❖ In next slide, we will do these tasks.
Software IndustrySultan Ahmed Sagor
Routes Setup
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { UserListComponent } from './user-list/user-list.component';
import { UserComponent } from './user/user.component’;
import { RouterModule, Routes } from '@angular/router’;
const appRoutes: Routes = [
{path: 'home', component: HomeComponent},
{path: 'about', component: AboutComponent}
];
Software IndustrySultan Ahmed Sagor
Routes Setup
❖ We have defined the routes in our app.module.ts but we need to also import the route in
the @NgModule
// app.module.ts
@NgModule({
declarations: [
AppComponent,
HomeComponent,
AboutComponent,
UserListComponent,
UserComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(appRoutes)
],
Software IndustrySultan Ahmed Sagor
Routes Setup
❖ We need to load all our new routes in the app.component.html.
❖ Inside the <router-outlet></router-outlet> tag is where our project content loads in the file named
app.component.html.
❖ We also need to create our navigation links. Instead of using the typical html’s href in the a tag, it
should be replaced with routerLink.
❖ The routerLink will link to the paths we earlier configured in the app.modules.ts
Software IndustrySultan Ahmed Sagor
Routes Setup
// app.component.html<ul>
<ul>
<li>
<a routerLink="/home">Home</a>
</li>
<li>
<a routerLink="/about">About</a>
</li>
</ul>
<router-outlet></router-outlet>
Software IndustrySultan Ahmed Sagor
Routes Setup
❖ With the above solution,
❖ if we click on the home link, it navigates to the Home-page,
❖ while if we click on the about link, it navigates to the About-page.
❖ But what happens when we browse to an empty url path localhost:4200 or an unavailable url
path localhost:4200/sultan?
❖ We won’t have any content loaded on the page because neither the empty path nor the unavailable
path has been assigned a route yet.
❖ So, let’s automatically make our empty/unavailable route navigate to the homepage. We will edit
the app.module.ts to achieve this.
Software IndustrySultan Ahmed Sagor
Routes Setup
// app.module.ts
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { UserListComponent } from './user-list/user-list.component';
import { UserComponent } from './user/user.component’;
import { RouterModule, Route } from '@angular/router’;
const appRoutes = [
{path: 'home', component: HomeComponent},
{path: 'about', component: AboutComponent},
//the empty path will be redirected to the home component
{path: '', redirectTo: '/home', pathMatch: 'full’},
//this path redirects to the home component
{path: '**', redirectTo: '/home', pathMatch: 'full’}
];
Software IndustrySultan Ahmed Sagor
Sub-route creation
Now we will create sub-routes which will be in the
user-list component and the list will contain users.
Software IndustrySultan Ahmed Sagor
Sub-routes creation
// app.module.ts
import { RouterModule, Route } from '@angular/router’;
const appRoutes = [
{path: 'home', component: HomeComponent},
{path: 'about', component: AboutComponent},
//the empty path will be redirected to the home component
{path: '', redirectTo: '/home', pathMatch: 'full’},
//this path redirects to the home component
{path: '**', redirectTo: '/home', pathMatch: 'full’},
{path: 'user', children: [
{path: 'list', component: UserListComponent}
]}
];
Software IndustrySultan Ahmed Sagor
Sub-routes cration
❖ In the above snippets, we have a path called “user” with children.
❖ The children is an array where we’ll define the children’s user list.
❖ So when we click on the “user” link, it should navigate to localhost:4200/user/list and open the page
in the UserListComponent.
❖ The So, let’s add some users to our user-list.component.ts. .
Software IndustrySultan Ahmed Sagor
Sub-routes creation
// user-list.component.ts
import { Component, OnInit } from '@angular/core’;
@Component({
selector: 'app-user-list’,
templateUrl: './user-list.component.html’,
styleUrls: ['./user-list.component.css']
})
export class UserListComponent implements OnInit { // adding some users
users = [
{ name: 'Maryann' },
{ name: 'Nife’ }
];
constructor() { }
ngOnInit() {}
}
Software IndustrySultan Ahmed Sagor
Sub-routes creation
❖ In the user-list.component.html, we need to add these names using *ngFor in a list tag.
<div class="container">
<h2>UserList Page</h2>
<ul class="users">
<li *ngFor="let user of users" [routerLink]="[
'detail', user.name]">{{user.name}}
</li>
</ul>
</div>
Software IndustrySultan Ahmed Sagor
Sub-routes Creation
❖ With the above solution, We need to configure this new router-outlet in the app.module.ts
❖ In the app.module.ts , add children to the UserListComponent route.
❖ This new children route will be an array of the new user’s detail path.
❖ The new user path will pass a parameter;
❖ in this case, “name” as seen in {path: ‘detail/:name’, component: UserComponent}
Software IndustrySultan Ahmed Sagor
Sub-routes creation
// app.module.ts.....
import { RouterModule, Route } from '@angular/router’;
const appRoutes = [
{path: 'home', component: HomeComponent},
{path: 'about', component: AboutComponent},
{path: '', redirectTo: '/home', pathMatch: 'full’},
{path: '**', redirectTo: '/home', pathMatch: 'full'},//this 'user' path has children
{path: 'user', children: [
{path: 'list', component: UserListComponent, children: [
{path: 'detail/:name', component: UserComponent}
]}
]}
];
Software IndustrySultan Ahmed Sagor
Sub-routes Creation
❖ When the user name renders, it needs to have a name pass through to the user page.
❖ So, in the user.component.ts, we need to grab parameters in the routerLink from user-
list.component.html .
❖ In the user.component.ts , we need to import ActivatedRoute, and activate new parameters.
Software IndustrySultan Ahmed Sagor
Sub-routes creation
// user.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router’;
@Component({
selector: 'app-user’,
templateUrl: './user.component.html’,
styleUrls: ['./user.component.css']
})
}
export class UserComponent implements OnInit {
name: any;
sub: any;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.sub = this.route.params.subscribe(params
=> {
this.name = params['name’];
});
}
}
Software IndustrySultan Ahmed Sagor
Sub-routes creation
// user.component.html
<div class="container">
<h2>User Page</h2>
<h3>Name: {{name}}</h3>
</div>
Software IndustrySultan Ahmed Sagor
Any
question?
Software IndustrySultan Ahmed Sagor
Thank You

More Related Content

What's hot

Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency InjectionNir Kaufman
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectJadson Santos
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
Angular 8
Angular 8 Angular 8
Angular 8 Sunil OS
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event HandlingWebStackAcademy
 
Angular Notes.pdf
Angular Notes.pdfAngular Notes.pdf
Angular Notes.pdfsagarpal60
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 
Angular data binding
Angular data binding Angular data binding
Angular data binding Sultan Ahmed
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorialRohit Gupta
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsWebStackAcademy
 
Angular App Presentation
Angular App PresentationAngular App Presentation
Angular App PresentationElizabeth Long
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js ExpressEyal Vardi
 
Building blocks of Angular
Building blocks of AngularBuilding blocks of Angular
Building blocks of AngularKnoldus Inc.
 
Creating custom Validators on Reactive Forms using Angular 6
Creating custom Validators on Reactive Forms using Angular 6Creating custom Validators on Reactive Forms using Angular 6
Creating custom Validators on Reactive Forms using Angular 6AIMDek Technologies
 

What's hot (20)

Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency Injection
 
Angular 9
Angular 9 Angular 9
Angular 9
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular 8
Angular 8 Angular 8
Angular 8
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Angular Notes.pdf
Angular Notes.pdfAngular Notes.pdf
Angular Notes.pdf
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Angular data binding
Angular data binding Angular data binding
Angular data binding
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
 
Express js
Express jsExpress js
Express js
 
Express JS
Express JSExpress JS
Express JS
 
Angular
AngularAngular
Angular
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
 
Angular App Presentation
Angular App PresentationAngular App Presentation
Angular App Presentation
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Building blocks of Angular
Building blocks of AngularBuilding blocks of Angular
Building blocks of Angular
 
Creating custom Validators on Reactive Forms using Angular 6
Creating custom Validators on Reactive Forms using Angular 6Creating custom Validators on Reactive Forms using Angular 6
Creating custom Validators on Reactive Forms using Angular 6
 

Similar to Angular routing

Angular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamAngular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamShubham Verma
 
8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!Laurent Duveau
 
ngNewRouter
ngNewRouterngNewRouter
ngNewRouterphidong
 
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Frost
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And NavigationEyal Vardi
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing optionsNir Kaufman
 
Angular components
Angular componentsAngular components
Angular componentsSultan Ahmed
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfKaty Slemon
 
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...Katy Slemon
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2Paras Mendiratta
 
Dependency Injection pattern in Angular
Dependency Injection pattern in AngularDependency Injection pattern in Angular
Dependency Injection pattern in AngularAlexe Bogdan
 
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...OdessaJS Conf
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverSpike Brehm
 
Peggy angular 2 in meteor
Peggy   angular 2 in meteorPeggy   angular 2 in meteor
Peggy angular 2 in meteorLearningTech
 
Neoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito
 
Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...Katy Slemon
 
Understand routing in angular 2
Understand routing in angular 2Understand routing in angular 2
Understand routing in angular 2codeandyou forums
 
Angular resolver tutorial
Angular resolver tutorialAngular resolver tutorial
Angular resolver tutorialKaty Slemon
 

Similar to Angular routing (20)

Angular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamAngular2RoutingSetupByShubham
Angular2RoutingSetupByShubham
 
8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!
 
ngNewRouter
ngNewRouterngNewRouter
ngNewRouter
 
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Angular components
Angular componentsAngular components
Angular components
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
 
Angular2 routing
Angular2 routingAngular2 routing
Angular2 routing
 
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2
 
Dependency Injection pattern in Angular
Dependency Injection pattern in AngularDependency Injection pattern in Angular
Dependency Injection pattern in Angular
 
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
 
Peggy angular 2 in meteor
Peggy   angular 2 in meteorPeggy   angular 2 in meteor
Peggy angular 2 in meteor
 
Neoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito — Routing and navigation in Angular
Neoito — Routing and navigation in Angular
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...Understanding router state in angular 7 passing data through angular router s...
Understanding router state in angular 7 passing data through angular router s...
 
Understand routing in angular 2
Understand routing in angular 2Understand routing in angular 2
Understand routing in angular 2
 
Angular resolver tutorial
Angular resolver tutorialAngular resolver tutorial
Angular resolver tutorial
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

Angular routing

  • 1. Software IndustrySultan Ahmed Sagor Angular 7 A framework for Presentation Layer
  • 3. Software IndustrySultan Ahmed Sagor Angular Routing
  • 4. Software IndustrySultan Ahmed Sagor Angular Tutorial: Road Covered So Far
  • 5. Software IndustrySultan Ahmed Sagor What is Angular Routing ? Now we will discuss angular routing
  • 6. Software IndustrySultan Ahmed Sagor What are Angular Router? ❖ Angular Router is vital part of any application. ❖ Loads the corresponding components with respect to the requested route. ❖ Allows us to control different routes, data and the components that render the data.
  • 7. Software IndustrySultan Ahmed Sagor Demo Now we will understand Router through Demo
  • 8. Software IndustrySultan Ahmed Sagor Highlights of Demo ❖ In this demo, we will do the following tasks: ❖ A page with 3 routes (Home, About, User) ❖ We should be able to navigate to the page assigned to each route. ❖ On our “User” page, we should have a sub-route that navigates to a user page; all in the main “User” page. ❖ Also, if we don’t provide any url route, it should navigate to the “homepage” automatically. ❖ And if we type in a garbage url route (a url that doesn’t exist), it should also navigate to the homepage as well or a 404 page.
  • 9. Software IndustrySultan Ahmed Sagor Component Generation Now we will create 3 components
  • 10. Software IndustrySultan Ahmed Sagor Component Generation ❖ Let us generate the components home, about, userList & user as the following syntax ❖ ng generate component home ❖ ng generate component about ❖ ng generate component userList ❖ ng generate component user
  • 11. Software IndustrySultan Ahmed Sagor Component Generation
  • 12. Software IndustrySultan Ahmed Sagor Routes Setup Now we will setup routes
  • 13. Software IndustrySultan Ahmed Sagor Routes Setup ❖ Let’s set up the routes. The routes will be set up in app.module.ts. ❖ Inside this file, all the 4 components we created earlier have been imported. // app.module.ts import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { UserListComponent } from './user-list/user-list.component'; import { UserComponent } from './user/user.component';
  • 14. Software IndustrySultan Ahmed Sagor Routes Setup ❖ The 4 components have also been declared in the @NgModule declaration in our app.module.ts file. ❖ basically, this helps us set the initial component property. // app.module.ts @NgModule({ declarations: [ AppComponent, HomeComponent, AboutComponent, UserListComponent, UserComponent ],
  • 15. Software IndustrySultan Ahmed Sagor Routes Setup ❖ We will need to import the router from the angular route library. ❖ Now that we have imported the router, the routes have to be configured by creating an object (const appRoutes..). ❖ This object will have all the information about the url path, components and redirect path for the routes. ❖ In next slide, we will do these tasks.
  • 16. Software IndustrySultan Ahmed Sagor Routes Setup import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { UserListComponent } from './user-list/user-list.component'; import { UserComponent } from './user/user.component’; import { RouterModule, Routes } from '@angular/router’; const appRoutes: Routes = [ {path: 'home', component: HomeComponent}, {path: 'about', component: AboutComponent} ];
  • 17. Software IndustrySultan Ahmed Sagor Routes Setup ❖ We have defined the routes in our app.module.ts but we need to also import the route in the @NgModule // app.module.ts @NgModule({ declarations: [ AppComponent, HomeComponent, AboutComponent, UserListComponent, UserComponent ], imports: [ BrowserModule, RouterModule.forRoot(appRoutes) ],
  • 18. Software IndustrySultan Ahmed Sagor Routes Setup ❖ We need to load all our new routes in the app.component.html. ❖ Inside the <router-outlet></router-outlet> tag is where our project content loads in the file named app.component.html. ❖ We also need to create our navigation links. Instead of using the typical html’s href in the a tag, it should be replaced with routerLink. ❖ The routerLink will link to the paths we earlier configured in the app.modules.ts
  • 19. Software IndustrySultan Ahmed Sagor Routes Setup // app.component.html<ul> <ul> <li> <a routerLink="/home">Home</a> </li> <li> <a routerLink="/about">About</a> </li> </ul> <router-outlet></router-outlet>
  • 20. Software IndustrySultan Ahmed Sagor Routes Setup ❖ With the above solution, ❖ if we click on the home link, it navigates to the Home-page, ❖ while if we click on the about link, it navigates to the About-page. ❖ But what happens when we browse to an empty url path localhost:4200 or an unavailable url path localhost:4200/sultan? ❖ We won’t have any content loaded on the page because neither the empty path nor the unavailable path has been assigned a route yet. ❖ So, let’s automatically make our empty/unavailable route navigate to the homepage. We will edit the app.module.ts to achieve this.
  • 21. Software IndustrySultan Ahmed Sagor Routes Setup // app.module.ts import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { UserListComponent } from './user-list/user-list.component'; import { UserComponent } from './user/user.component’; import { RouterModule, Route } from '@angular/router’; const appRoutes = [ {path: 'home', component: HomeComponent}, {path: 'about', component: AboutComponent}, //the empty path will be redirected to the home component {path: '', redirectTo: '/home', pathMatch: 'full’}, //this path redirects to the home component {path: '**', redirectTo: '/home', pathMatch: 'full’} ];
  • 22. Software IndustrySultan Ahmed Sagor Sub-route creation Now we will create sub-routes which will be in the user-list component and the list will contain users.
  • 23. Software IndustrySultan Ahmed Sagor Sub-routes creation // app.module.ts import { RouterModule, Route } from '@angular/router’; const appRoutes = [ {path: 'home', component: HomeComponent}, {path: 'about', component: AboutComponent}, //the empty path will be redirected to the home component {path: '', redirectTo: '/home', pathMatch: 'full’}, //this path redirects to the home component {path: '**', redirectTo: '/home', pathMatch: 'full’}, {path: 'user', children: [ {path: 'list', component: UserListComponent} ]} ];
  • 24. Software IndustrySultan Ahmed Sagor Sub-routes cration ❖ In the above snippets, we have a path called “user” with children. ❖ The children is an array where we’ll define the children’s user list. ❖ So when we click on the “user” link, it should navigate to localhost:4200/user/list and open the page in the UserListComponent. ❖ The So, let’s add some users to our user-list.component.ts. .
  • 25. Software IndustrySultan Ahmed Sagor Sub-routes creation // user-list.component.ts import { Component, OnInit } from '@angular/core’; @Component({ selector: 'app-user-list’, templateUrl: './user-list.component.html’, styleUrls: ['./user-list.component.css'] }) export class UserListComponent implements OnInit { // adding some users users = [ { name: 'Maryann' }, { name: 'Nife’ } ]; constructor() { } ngOnInit() {} }
  • 26. Software IndustrySultan Ahmed Sagor Sub-routes creation ❖ In the user-list.component.html, we need to add these names using *ngFor in a list tag. <div class="container"> <h2>UserList Page</h2> <ul class="users"> <li *ngFor="let user of users" [routerLink]="[ 'detail', user.name]">{{user.name}} </li> </ul> </div>
  • 27. Software IndustrySultan Ahmed Sagor Sub-routes Creation ❖ With the above solution, We need to configure this new router-outlet in the app.module.ts ❖ In the app.module.ts , add children to the UserListComponent route. ❖ This new children route will be an array of the new user’s detail path. ❖ The new user path will pass a parameter; ❖ in this case, “name” as seen in {path: ‘detail/:name’, component: UserComponent}
  • 28. Software IndustrySultan Ahmed Sagor Sub-routes creation // app.module.ts..... import { RouterModule, Route } from '@angular/router’; const appRoutes = [ {path: 'home', component: HomeComponent}, {path: 'about', component: AboutComponent}, {path: '', redirectTo: '/home', pathMatch: 'full’}, {path: '**', redirectTo: '/home', pathMatch: 'full'},//this 'user' path has children {path: 'user', children: [ {path: 'list', component: UserListComponent, children: [ {path: 'detail/:name', component: UserComponent} ]} ]} ];
  • 29. Software IndustrySultan Ahmed Sagor Sub-routes Creation ❖ When the user name renders, it needs to have a name pass through to the user page. ❖ So, in the user.component.ts, we need to grab parameters in the routerLink from user- list.component.html . ❖ In the user.component.ts , we need to import ActivatedRoute, and activate new parameters.
  • 30. Software IndustrySultan Ahmed Sagor Sub-routes creation // user.component.ts import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router’; @Component({ selector: 'app-user’, templateUrl: './user.component.html’, styleUrls: ['./user.component.css'] }) } export class UserComponent implements OnInit { name: any; sub: any; constructor(private route: ActivatedRoute) { } ngOnInit() { this.sub = this.route.params.subscribe(params => { this.name = params['name’]; }); } }
  • 31. Software IndustrySultan Ahmed Sagor Sub-routes creation // user.component.html <div class="container"> <h2>User Page</h2> <h3>Name: {{name}}</h3> </div>
  • 32. Software IndustrySultan Ahmed Sagor Any question?
  • 33. Software IndustrySultan Ahmed Sagor Thank You