All TalkersCode Topics

Follow TalkersCode On Social Media

Single Page Application In Angularjs

Last Updated : Jan 1, 2023

Single Page Application In Angularjs

In this tutorial we will show you the solution of single page application in angularjs, single page application means each HTML pages we defined will updated on single HTML page and this concept done by ‘ngRoute’.

Using angularjs route library supports we can set routes in html template for each html pages then when we clicks on particular links it loads which page we sets to be displayed on webpage.

Step By Step Guide On Single Page Application In Angularjs :-

In our application we imported ‘angular support libs and angular route support libs’ for get service of routing.

We defined two anchor tags to link other html pages and in controller we defined ‘ngRoute’ with application then using ‘$routeProvider’ we sets route for each pages with different controller.

Controller appends with application respective controller name within that we defined message for each page those string will display on respective pages.

<!DOCTYPE html>
<html ng-app="myapp">
<title>Single page web app using Angularjs</title>
<script src=""></script>
<script src=""></script>
<a href="#/home">Home</a>
<a href="#/about">About</a>
<div ng-view></div>
<script src="app.js"></script>
<h1>Hello, !{{msg}}</h1>
<h1>About us page.</h1>
<p>This is executed because of this code.</p>
var app=angular.module('myapp',['ngRoute']);
            templateUrl: 'down/home.html',
            controller: 'ctrl'
            templateUrl: 'down/home.html',
            controller: 'homeCtrl'
            templateUrl: 'down/about.html',
            controller: 'aboutCtrl'
      $scope.msg="This is index page.";
    $scope.msg=" This is home page. ";
      $scope.msg=" This is about page. ";
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we need to import two script angularjs files namely ‘angular,angular-route’ because without those files we can’t run our application to be successfully.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contains coding of entire website blocks and elements described here. In angularjs we need’s to define our application and controller using directives of ‘ng-app and ng-controller’ with respective values ‘myapp and ctrl’.
  7. Here we defined two <a> anchor tags contain ‘home and about’ pages link and must need to define div tag with ‘ng-view’ directive because our route files are appends on ‘ng-view’ with our application.
  8. In script ‘app.js’ we defined ‘$routeProvider’ is creates the $route service. By configuring the ‘$routeProvider’ before the $route service we can set routes in HTML templates which will be displayed.
  9. Using that we sets route for page ‘home and about’ in ‘templateUrl’ with different controllers ‘ctrl, homeCtrl, aboutCtrl’. Finally we added otherwise call because it is used to configure ‘$routeProvider’.
  10. In application we appends each controller with $scope object to define some message that will be display on respective pages.
  11. Both </body> ,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to create single page application using angularjs.

When we executing our program on browser we need to start server first then here we used some open source libraries so need internet connection also.

In browser we can see two links of ‘Home, About’ we defined in application with one message we defined in controller also displayed.

When we click’s on ‘Home or About’ page links at same frame we can see respective clicked page layouts.

I hope this tutorial on single page application in angularjs helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials