Unit 4
Unit 4
Questions
A)Explain about routing in angular with suitable example?
1
B)Describe about $routeProvider with suitable example.
2 A)How to create routes in angular? Describe it with an example.
A)Explain about route redirect with suitable example.
3
B)How to identify active route? Explain it with suitable example.
A)How to configure child routes in angular? Describe it with an example.
4
B)What are Route Parameters? Explain.
A) How to navigate for routes in angular? Explain it with an example.
5
B)How to setup wildcards? Explain it with an example.
A)Explain Router Query Parameters with example.
6
A)How to build a single-page application using Angular routes?
7
B) Describe in detail about the Angular CLI?
A)How to add Routing to an Existing Angular Project explain with example?
8
B)Explain the Router configuration in the application module with relevant example?
Ans)
In a single-page app, you change what the user sees by showing or hiding portions of the
display that correspond to particular components, rather than going out to the server to get a
new page. As users perform application tasks, they need to move between the
different views that you have defined.
To handle the navigation from one view to the next, you use the Angular Router.
The Router enables navigation by interpreting a browser URL as an instruction to change the
view.
Routing Example
We will be building an application, which will display a login page when a user requests
for base url - http://localhost/. Once the user logs in successfully, we will redirect it to
student page http://localhost/student/{username} where username would be logged in
user's name.
In our example, we will have one layout page - index.html, and two HTML templates -
login.html and student.html.
Ans)
Routing is allows us create Single Page Applications. To do this, we use ng-view and ng-
template directives, and $routeProvider services.
We use $routeProvider to configure the routes.
The config() takes a function which takes the $routeProvider as parameter and the routing
configuration goes inside the function.
$routeProvider is a simple API which which accepts either when() or otherwise() method.
By configuring the $routeProvider before the $route service we can set routes in HTML
templates which will be displayed.
The $routeProvider is configured with the help of calls to the when() and otherwise()
functions.
app.config(function($routeProvider) {
$routeProvider
.when('/1stview', {
templateUrl: '1stview.html',
controller: 'Controller1'
})
.when('/view2', {
templateUrl: '2ndview.html',
controller: 'Controller2'
})
.otherwise({
redirectTo: '/1stview'
});
});
1. templateUrl
2. controller
The $routeProvider can be used to define the page when the user clicks the link.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js ">
</script>
<script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-route.js ">
</script>
<body ng-app="myApp">
<p><a href="#/!"> GFG</a></p>
<p>Click on the links below.</p>
<div ng-view></div>
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "main.htm",
})
.when("/C", {
templateUrl: "C.htm",
})
.when("/C++", {
templateUrl: "C++.htm",
});
});
</script>
</body>
</html>
Routing in Angular allows the users to create a single-page application with multiple views
and allows navigation between them. Users can switch between these views without losing
the application state and properties.
Approach:
Create an Angular app that to be used.
Create the navigation links inside the app component and then provide
the “routerLink” directive to each route and pass the route value
to “routerLink” directive.
Then add the routes to the routing.module.ts file and then import the
routing.module.ts into the app.module.ts file.
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}