JavaScript - Simple Route Configuration

In this tutorial we will create a Simple Route Configuration using AngularJS. AngularJS is a structural framework for dynamic web apps. It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

Getting started:

First you will need to download & install the AngularJS here's the link https://angularjs.org/. And download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Note: To make the angular-route.js work make sure you are running this application to any local server like xamp, etc..

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as shown below. index.html
  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Simple Route Configuration</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <ul class="nav nav-pills">
  17.                         <li><a href="#!">Home</a></li>
  18.                         <li><a href="#!about">About us</a></li>
  19.                         <li><a href="#!subject">Subject</a></li>
  20.                 </ul>
  21.        
  22.                 <hr style="border-top:1px solid #ccc;"/>
  23.                 <ng-view></ng-view>
  24.         </div>
  25. <script src="js/angular.js"></script>
  26. <script src="js/angular-route.js"></script>
  27. <script src="js/script.js"></script>
  28. </body>
  29. </html>
home.html
  1. <h1>Home</h1>
subject.html
  1. <h1>Subject</h1>
about.html
  1. <h1>About us</h1>

Creating the Script

This code contains the main function of the application. This code will create a path that handle the web pages using angular-route.js. To that just kindly copy and write these block of codes inside the text editor, then save it as script.js.
  1. var app = angular.module('app', ['ngRoute']);
  2.  
  3.         app.config(function($routeProvider){
  4.                 $routeProvider
  5.                 .when('/', {
  6.                         templateUrl: 'home.html'
  7.                 })
  8.                 .when('/about', {
  9.                         templateUrl: 'about.html'
  10.                 })
  11.                 .when('/subject', {
  12.                         templateUrl: 'subject.html'
  13.                 })
  14.         });
There you have it we successfully created a Simple Route Configuration using AngularJS. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment