Single Page Web Applications using AngularJS

Single Page Web Applications using AngularJS

  • 2016-08-12
  • 2926

AngularJs provides ng-route and ng-view directive and by using them we can easily develop our single page web app. In this tutorial i am going to show you how to develop simple one page web application using AngularJs.

ng-route manage routing “Express Complete Tutorial : Part 3”) of web application. By routing i mean when you click on link in ordinary web page you get redirected to target anchor link. By using ng-route we can handle those routes.



ng-view allow us to render different templates on particular view depending upon the router. For example, if user hits then route will tell to load particular template for this route.

Our project:

We are building simple web app, we are consist of following files.

---- index.html ( Starting page)
---- home.html  ( template )
---- about.html ( template )
---- script.js  ( Angular code )

Here is how it works.

enter image description here

Before explanation let me show you the code. First have a look at index.html.


<!doctype html>
<html lang="en">
  <meta charset="UTF-8">
  <title>Single page web app using Angularjs</title>
  <script src="//"></script>
  <script src="//">   </script>    
<script src="script.js"></script>
<body ng-app="single-page-app">
  <div ng-controller="cfgController">
        <li><a href="#/">Home</a></li>
        <li><a href="#/about">About us</a></li>
  <div ng-view>
     This DIV loads templates depending upon route.

Here is our angular code. Responsible for handling routes and updating view.


var app=angular.module('single-page-app',['ngRoute']);
                templateUrl: 'home.html'
                templateUrl: 'about.html'

    Here you can handle controller for specific route as well.

If you have noticed $routeProvide here we are loading home and about HTML files into view. Let’s have a look on to them too.


<h1>Hello, world!</h1>
    This is executed because of this code.
              templateURl: 'home.html'


<h1>About us page.</h1>
   This is executed because of this code.
             templateURl: 'about.html'


The main code which does the magic is this.

                templateUrl: 'home.html'
                templateUrl: 'about.html'

Depending upon your templates and routes you can add more in it. As you see, when particular URL hits, we simply passing our file URL and Angular automatically Loads it into ng-view.

How to run the code:

Download file, serve it using any web server, for ease you can use XAMPP package to install web server. Put directory into HTDOCS folder and run through browser.


Single page web application is very intuitive and easy to use. In this, web app used to load once and then only particular part of app keeps on changing without refreshing whole page which saves bandwidth as well as no loading of external files again and again such as Images or CSS files which in most of web app is static.


Angularjs 2 with TypeScript Tutorial for Beginners

AngularJS 2.0: What to Expect and Interesting?

Building Line Of Business Apps With Isomorphic React/Node

Angular 2 - The Complete Guide (Updated to RC4!)

Angular 2 with TypeScript for Beginners: The Pragmatic Guide

Angular 2 Fundamentals: Learn By Creating A Real Web App