Single Page Application in AngularJS

DZone 's Guide to

Single Page Application in AngularJS

Single page applications are steadily regaining popularity, since not every site needs to be dynamic. Here's how to create on in AngularJS.

· Web Dev Zone ·
Free Resource

AngularJS at this point in time is so famous that probably all of us have heard of or tried it by now. It is the popularity of this technology that’s forcing most eCommerce websites to shift to it. 

The benefits provided 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,

Image title

Step 2

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


Image title


Image title

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. 

/// <reference path="angular.js" />    
//we have created an app for the ng-app mentioned in our page and included the ngRoute for routing configuration  
var myApp = angular.module("myApp", ['ngRoute']);  
//start configuring routes  
myApp.config(['$routeProvider', function($routeProvider)  
    //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  
        templateUrl: 'Pages/home.html',  
        controller: 'homeCtrl'  
    //when the requested url is "/about" then call the view "about.html" and assign the controller "aboutCtrl" to it  
        templateUrl: 'Pages/about.html',  
        controller: 'aboutCtrl'  
    //when the requested url is '/contact then call the view "contact.html" and assign the controller "contactCtrl" to it  
        templateUrl: 'Pages/contact.html',  
        controller: 'contactCtrl'  
    //in case any other url is requested then direct the user to home page  
        redirectTo: '/'  

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 myApp.js and code it as below. 

myApp.controller('homeCtrl', ['$scope', function ($scope) {  
    $scope.message = "this is message from Home Page controller";  


myApp.controller('aboutCtrl', ['$scope', function ($scope) {  
    $scope.message = "this is message from About Page controller";  


myApp.controller('contactCtrl', ['$scope', function ($scope) {  
    $scope.message = "this is message from Contact Page controller";  


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.

<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="myApp">  
    <title>AngularJs Single Page App</title>  
    <script src="Scripts/angular.js"></script>  
    <script src="Scripts/angular-route.js"></script>  
    <script src="Scripts/config.js"></script>  
    <script src="Scripts/myApp.js"></script>  
    <link href="Css/site.css" rel="stylesheet" />  

        <div class="content-wrapper">  

            <div class="float-right">  
                    <ul id="menu">  
                        <li><a href="#/">Home</a></li>  
                        <li><a href="#/about">About</a></li>  
                        <li><a href="#/contact">Contact</a></li>  
    <div id="body">  
          <div ng-view></div>  



The final project structure is as below.

Image title

Step 7

We run this application and the output is as below.


Image title


Image title


Image title

angular js, javascript, single page application

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}