Landing Page

In this example, we are having the following three hyperlinks available at the top of the page: Home, About & Contact.
By default Home.html is landing page as per our code.

When we click About hyperlink, it navigates to the About.html page.

When we click Contact hyperlink, it navigates to the Contact.html page.

home page

Index.html

As per our html design shown below, each hyperlink point to the below mentioned URL when it is running locally.

In order to perform routing we need to include AngularJS specific JavaScript file reference as given:

  1. <script src="../Scripts/angular-route.min.js"></script>
Source Code:
  1. <!DOCTYPE html>
  2. <!-- define angular app -->
  3. <html ng-app="RoutingApp">
  4. <head>
  5. <!-- CSS Files -->
  6. <link rel="stylesheet" href="../css/bootstrap.min.css" />
  7. <link rel="stylesheet" href="../css/font-awesome.css" />
  8. <!-- SPELLS -->
  9. <script src="../Scripts/angular.min.js"></script>
  10. <script src="../Scripts/angular-route.min.js"></script>
  11. <script src="PageRouting.js"></script>
  12. </head>
  13. <body ng-controller="mainController">
  14. <nav class="navbar navbar-default">
  15. <div class="container">
  16. <div class="navbar-header">
  17. <a class="navbar-brand" href="/">
  18. Angular Routing Example
  19. </a>
  20. </div>
  21. <ul class="nav navbar-nav navbar-right">
  22. <li><a href="#"><i class="fa fa-home"></i>Home</a></li>
  23. <li><a href="#about"><i class="fa fa-shield"></i>About</a></li>
  24. <li><a href="#contact"><i class="fa fa-comment"></i>Contact</a></li>
  25. </ul>
  26. </div>
  27. </nav>
  28. <div id="main">
  29. <!-- angular templating : This is where content will be injected -->
  30. <div ng-view></div>
  31. </div>
  32. </body>
  33. </html>
PageRouting.js

For Routing we need to add ' ngRoute' dependent module and we need to configure our route as in the following code snippet:
  1. // create the module and name it scotchApp
  2. var scotchApp = angular.module('RoutingApp', ['ngRoute']);
  3. // configure our routes
  4. scotchApp.config(function($routeProvider) {
  5. $routeProvider
  6. // route for the home page
  7. .when('/', {
  8. templateUrl: 'pages/home.html',
  9. controller: 'mainController'
  10. })
  11. // route for the about page
  12. .when('/about', {
  13. templateUrl: 'pages/about.html',
  14. controller: 'aboutController'
  15. })
  16. // route for the contact page
  17. .when('/contact', {
  18. templateUrl: 'pages/contact.html',
  19. controller: 'contactController'
  20. });
  21. });
  22. // create the controller and inject Angular's $scope
  23. scotchApp.controller('mainController', function($scope) {
  24. // create a message to display in our view
  25. $scope.HomeMessage = 'Home Controller Called !!!';
  26. });
  27. scotchApp.controller('aboutController', function($scope) {
  28. $scope.AboutMessage = 'About Controller Called !!!';
  29. });
  30. scotchApp.controller('contactController', function($scope) {
  31. $scope.ContactMessage = 'Contact Controller Called !!!';
  32. });
Home.html

When 'http://localhost:50890/RoutingSinglePage/index.html#/' hits in the browser, home.html file gets loaded.
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <div>
  8. <h1>Home Page</h1> <br />
  9. <p>{{ HomeMessage }}</p>
  10. </div>
  11. </body>
  12. </html>
About.html

When 'http://localhost:50890/RoutingSinglePage/index.html#/about' hits in the browser, about.html file gets loaded.
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <div>
  8. <h1>About Page</h1>
  9. <br />
  10. <p>{{ AboutMessage }}</p>
  11. </div>
  12. </body>
  13. </html>
Contact.html

When 'http://localhost:50890/RoutingSinglePage/index.html#/contact' hits in the browser, contact.html file gets loaded.
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <div>
  8. <h1>Contact Page</h1>
  9. <br />
  10. <p>{{ ContactMessage }}</p>
  11. </div>
  12. </body>
  13. </html>
Thank you! Please mention your queries if you have any.