Build Single Page Application Using AngularJS

Build a Single Page Application SPA Using AngularJS:


In this tutorial, we will get to know more about developing a single page application using AngularJS.

AngularJS is an open-source web application framework based on JavaScript.
It is maintained by Google Corporation and a big community. AngularJS is an answer to the various drawbacks that are encountered to formulate single-page applications.

Advantages of Single Page Applications

  • Improved user experience.
  • Web pages refresh faster as less bandwidth is being used.
  • The deployment of the application – index.html, CSS bundle, and javascript bundle – in production becomes easier.
  • Code splitting can be done to split the bundles into multiple parts.

How to Develop a Single Page Application Using AngularJS?

Nowadays, there are many javascript applications that are available in the market like ember.js, backbone.js, etc. But still, a lot of web applications incorporate AngularJS in their development to create SPAs.
Below are the various steps involved in developing a SPA using AngularJS.

Step 1: Create a Module

var app = angular.module("firstApp", ["ngRoute"]);

Step 2: Use AngularJS and AngularJS`s routing capabilities to add different views to our SPA
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular-route.js"></script>

Step 3: Create an HTML layout for the website (index.html)
<!DOCTYPE html>
<html lang="en-US">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular-route.js"></script>
<script src="spa.js"></script>
</head>
<body ng-app="firstApp">
<a href="#">Home</a>
<a href="#!blog">Blog</a>
<a href="#!about">About</a>
<div ng-view></div>
</body>
</html>

Step 4: Use $routeProvider service from ngRoute module to configure the navigation to different views (spa.js)
var app = angular.module('firstApp', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl : 'home.html',
controller : 'homeCtrl'
})
.when('/blog', {
templateUrl : 'blog.html',
controller : 'blogCtrl'
})
.when('/about', {
templateUrl : 'about.html',
controller : 'aboutCtrl'
})
.otherwise({redirectTo: '/'});
});

Step 5: Build controllers for every route specified in $routeProvider (spa.js)
app.controller('homeCtrl', function($scope) {
$scope.message = 'Home Page';
});
app.controller('blogCtrl', function($scope) {
$scope.message = 'Blog Page';
});
app.controller('aboutCtrl', function($scope) {
$scope.message = 'About Page';
});

Step 6: Configure the pages
home.html
<h1>Home</h1>
<h3>{{message}}</h3>

blog.html
<h1>Blog</h1>
<h3>{{message}}</h3>

about.html
<h1>About</h1>
<h3>{{message}}</h3>

Click Here to Download Code

If you have any query or suggestions, feel free to ask me via the comment section below.

2 Comments

Leave a Reply