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 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Edureka!
 
Basic overview of Angular
Basic overview of AngularBasic overview of Angular
Basic overview of AngularAleksei Bulgak
 
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 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
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular ComponentsSquash Apps Pvt Ltd
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentationThanh Tuong
 
Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes WorkshopNir Kaufman
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - IntroductionWebStackAcademy
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 
Angular components
Angular componentsAngular components
Angular componentsSultan Ahmed
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JSArno Lordkronos
 

What's hot (20)

Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
 
Basic overview of Angular
Basic overview of AngularBasic overview of Angular
Basic overview of Angular
 
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 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
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes Workshop
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
React workshop
React workshopReact workshop
React workshop
 
jQuery
jQueryjQuery
jQuery
 
Angular overview
Angular overviewAngular overview
Angular overview
 
Angular components
Angular componentsAngular components
Angular components
 
Express js
Express jsExpress js
Express js
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 

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
 
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
 
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
 
Angular 2
Angular 2Angular 2
Angular 2
 

Recently uploaded

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
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
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
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
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
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
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
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 

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