Single Page Application in AngularJS

AngularJS at this point of time is so famous that probably all of us have heard of/tried it by now.

It is the popularity of this technology that’s forcing most of the ecommerce websites to shift to it. 

The benefits provided by it are fabulous: fast routing, two-way data binding, etc.

Today we are going to try and make a Single Page application using AngularJS in Visual Studio 2013.

Step 1

Let’s create html for the Default page of our application. I am using the default layout of an MVC application as below,

 

Step 2

The next step is to create three partial views for the three links that we have here: Home, About and Contact. I would not be putting much data in it, just simple text providing some information about the page. Details are as below:

About 

 
 
Contact

 

Step 3

The next step is to create a configuration file (named config.js) which will handle all the routing that is needed for this application. The code of the file is as below. Please see through the comments as everything is pretty much well explained in it. 
  1. /// <reference path="angular.js" />    
  2. //we have created an app for the ng-app mentioned in our page and included the ngRoute for routing configuration  
  3. var myApp = angular.module("myApp", ['ngRoute']);  
  4. //start configuring routes  
  5. myApp.config(['$routeProvider', function($routeProvider)  
  6. {  
  7.     $routeProvider  
  8.     //when the home page is requested then call the view "home.html" and at the same time dynamically assign the controller "homeCtrl" to the page  
  9.     .when('/',  
  10.     {  
  11.         templateUrl: 'Pages/home.html',  
  12.         controller: 'homeCtrl'  
  13.     })  
  14.     //when the requested url is "/about" then call the view "about.html" and assign the controller "aboutCtrl" to it  
  15.     .when('/about',  
  16.     {  
  17.         templateUrl: 'Pages/about.html',  
  18.         controller: 'aboutCtrl'  
  19.     })  
  20.     //when the requested url is '/contact then call the view "contact.html" and assign the controller "contactCtrl" to it  
  21.     .when('/contact',  
  22.     {  
  23.         templateUrl: 'Pages/contact.html',  
  24.         controller: 'contactCtrl'  
  25.     })  
  26.     //in case any other url is requested then direct the user to home page  
  27.     .otherwise(  
  28.     {  
  29.         redirectTo: '/'  
  30.     });  
  31. }])  

The configuration had been done for the three urls that we expect to be requested including the html file that it will request and also the associated controller with it.

Step 4

Include the above generated file in your default.html page.

Step 5

In this step we will create another js file which will contain information regarding the controller of each of the pages mentioned above: Home, About, and Contact. Let’s name this file as myApp.js and let's code it as below. 

  1. myApp.controller('homeCtrl', ['$scope'function ($scope) {  
  2.     $scope.message = "this is message from Home Page controller";  
  3.   
  4. }]);  
  5.   
  6.   
  7. myApp.controller('aboutCtrl', ['$scope'function ($scope) {  
  8.     $scope.message = "this is message from About Page controller";  
  9.   
  10. }]);  
  11.   
  12. myApp.controller('contactCtrl', ['$scope'function ($scope) {  
  13.     $scope.message = "this is message from Contact Page controller";  
  14.   
  15. }]);  

Step 6

Please make sure that this file is included in the index.html i.e. our main page.

The final outcome of the index.html page is as below.

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="myApp">  
  3. <head>  
  4.     <title>AngularJs Single Page App</title>  
  5.     <script src="Scripts/angular.js"></script>  
  6.     <script src="Scripts/angular-route.js"></script>  
  7.     <script src="Scripts/config.js"></script>  
  8.     <script src="Scripts/myApp.js"></script>  
  9.     <link href="Css/site.css" rel="stylesheet" />  
  10. </head>  
  11. <body>  
  12.     <header>  
  13.   
  14.         <div class="content-wrapper">  
  15.               
  16.             <div class="float-right">  
  17.                 <nav>  
  18.                     <ul id="menu">  
  19.                         <li><a href="#/">Home</a></li>  
  20.                         <li><a href="#/about">About</a></li>  
  21.                         <li><a href="#/contact">Contact</a></li>  
  22.                     </ul>  
  23.                 </nav>  
  24.             </div>  
  25.         </div>  
  26.     </header>  
  27.     <div id="body">  
  28.           <div ng-view></div>  
  29.        
  30.     </div>    
  31.          
  32. </body>  
  33. </html>  
The final project structure is as below.

 

Step 7

We Run this application and the output is as below.

Home
 
 
About
 
 
Contact


Up Next
    Ebook Download
    View all
    Learn
    View all