Creating a Simple AngularJS Application with partials and ngRoute | JavaInUse

Creating a Simple AngularJS Application with ngRoute and Partials


This tutorial is explained in the below Youtube Video.


In this chapter we will create an AngularJS Application with Partials Views and ngRoute. AngularJS builds single page web applications. It creates web application where we have only one HTML page whose content can be changed in Javascript without having to download a new page. The new content is created programmatically in Javascript of with the use of templates. Have not used any css for this example

Lets Begin

Create a simple maven project as specified in Spring MVC Config chapter. Dont add any spring dependency in the created pom file.
Only add the build plugins required for maven and tomcat configurations. We will create Eclipse Maven project as follows-

Our pom.xml file is as follows-
<project xmlns="" xmlns:xsi=""
	<name>angular-hashbang Maven Webapp</name>

In the scripts folder angular.min.js and angular-route.min.js are the AngularJS Script files downloaded from the site.
We will create the script.js as follows-
var app = angular.module('blog', ['ngRoute']);

app.config(function($routeProvider,$locationProvider) {
			.when("/about", {
				templateUrl : "Templates/about.html"
			.when("/contact", {
				templateUrl : "Templates/contact.html"

Here angular.module defines a new module named blog. The module is a container for the different parts of an application like controllers. We use this module to bootstrap the index.html

Next we create the index.html page as follows. This will be the single main html page which will be used to dynamically load the content of other html templates
<!DOCTYPE html>
<html ng-app="blog">
<script src="scripts/angular.min.js"></script>
<script src="scripts/angular-route.min.js"></script>
<script src="scripts/script.js"></script>
	<table style="font-family: Arial">
			<td class="menu">
			<a href="#/about">About</a><br>
			<a href="#/contact">Contact</a></td>
			<td class="content">
			<div ng-view></div>

Here we have used
<html ng-app="blog">- AngularJS extends HTML with ng-directives. The ng-app directive defines an AngularJS application. It automatically initializes or bootstraps an AngularJS application. Here we are bootstrapping our AngularJS app with the blog app we created in script.js.
div ng-view- This AngularJS directive is the place where the partials get loaded dynamically.

Create the about.html in Templates folder. This is the template to be loaded in partial view.
This Site has been developed to help programmers learn and share knowledge. 
Efforts have been taken to attach screen shot and provide Hands on implementation Details. Any feedback is welcome. Hope it helps people learn. 

Create the contact.html in Templates folder. This is the template to be loaded in partial view.
You can contact us at

Now run the Maven commands-
clean install

Open the Browser and go to http://localhost:8080/angular-hashbang.

If we click the about It will open the page as below-

If we click the contact

Download Source Code

Download it - AngularJS Partials


See Also

Removing the # from the AngularJS URL Use Google reCaptcha with AngularJS Use AngularJS NgCloak directives Example PrimeNG-UI Components for AngularJS 2 Basic Example