Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • AngularJS Tutorial
  • AngularJS Directives
  • AngularJS Functions
  • AngularJS Filters
  • AngularJS Examples
  • AngularJS Interview Questions
  • Angular ngx Bootstrap
  • AngularJS Cheat Sheet
  • AngularJS PrimeNG
  • JavaScript
  • Web Technology
Open In App
Next Article:
Deployment of Angular Application using Github Pages
Next article icon

What is SPA (Single page application) in AngularJS ?

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Traditionally, applications were Multi-Page Applications (MPA) where with every click a new page would be loaded from the server. This was not only time-consuming but also increased the server load and made the website slower. AngularJS is a JavaScript-based front-end web framework based on bidirectional UI data binding and is used to design Single Page Applications. Single Page Applications are web applications that load a single HTML page and only a part of the page instead of the entire page gets updated with every click of the mouse. The page does not reload or transfer control to another page during the process. This ensures high performance and loading pages faster. Most modern applications use the concept of SPA. In the SPA, the whole data is sent to the client from the server at the beginning. As the client clicks certain parts on the webpage, only the required part of the information is fetched from the server and the page is rewritten dynamically. This results in a lesser load on the server and is cost-efficient. SPAs use AJAX and HTML5 to create fluid and responsive Web applications and most of the work happens on the client side. Popular applications such as Facebook, Gmail, Twitter, Google Drive, Netflix, and many more are examples of SPA. 

Advantages of Single Page Application:

  • Team collaboration: Single-page applications are excellent when more than one developer is working on the same project. It allows backend developers to focus on the API, while frontend developers can focus on creating the user interface based on the backend API.
  • Caching: The application sends a single request to the server and stores all the received information in the cache. This proves beneficial when the client has poor network connectivity.
  • Fast and responsive: As only parts of the pages are loaded dynamically, it improves the website’s speed.
  • Debugging is easier: Debugging single-page applications with chrome is easier since such applications are developed using AngularJS Batarang and React developer tools.
  • Linear user experience: Browsing or navigating through the website is easy.

Disadvantages of Single Page Application:

  • SEO optimization: SPAs provide poor SEO optimization. This is because single-page applications operate on JavaScript and load data at once server. The URL does not change and different pages do not have a unique URL. Hence it is hard for the search engines to index the SPA website as opposed to traditional server-rendered pages.
  • Browser history: A SPA does not save the users’ transition of states within the website. A browser saves the previous pages only, not the state transition. Thus when users click the back button, they are not redirected to the previous state of the website. To solve this problem, developers can equip their SPA frameworks with the HTML5 History API.
  • Security issues: Single-page apps are less immune to cross-site scripting (XSS) and since no new pages are loaded, hackers can easily gain access to the website and inject new scripts on the client-side.
  • Memory Consumption: Since the SPA can run for a long time sometimes hours at a time, one needs to make sure the application does not consume more memory than it needs. Else, users with low-memory devices may face serious performance issues.
  • Disabled Javascript: Developers need to chalk out ideas for users to access the information on the website for browsers that have Javascript disabled.

When to use SPA? 

SPAs are good when the volume of data is small and the website needs a dynamic platform. It is also a good option for mobile applications. But businesses that depend largely on search engine optimizations such as e-commerce applications must avoid single-page applications and opt for MPAs. 

Example: This example describes the basic demo of the Single Page Application in AngularJS.

HTML
<!DOCTYPE html> <!--ng-app directive tells AngularJS that myApp     is the root element of the application --> <html ng-app="myApp">  <head>     <!--import the angularjs libraries-->     <script src= "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js">     </script>     <script src= "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-route.min.js">     </script>      <style>         body {             text-align: center;             font-family: Arial, Helvetica, sans-serif;         }          h1 {             color: green;         }     </style> </head>  <body>     <h1>GeeksforGeeks</h1>     <h3>Single Page Application in AngularJS</h3>      <!--hg-template indicates the pages         that get loaded as per requirement-->     <script type="text/ng-template" id="first.html">         <h1>First Page</h1>         <h2 style="color:green">             Welcome to GeeksForGeeks         </h2>         <h3>{{message}}</h3>     </script>     <script type="text/ng-template" id="second.html">         <h1>Second Page</h1>         <h2 style="color:green">             Start Learning With GFG         </h2>         <h3>{{message}}</h3>     </script>     <script type="text/ng-template" id="third.html">         <h1>Third Page</h1>         <h2 style="color:green">             Know about us         </h2>         <h3>{{message}}</h3>     </script>      <!-- Hyperlinks to load different         pages dynamically -->     <a href="#/">First</a>     <a href="#/second">Second</a>     <a href="#/third">Third</a>      <!--ng-view includes the rendered template of         the current route into the main page-->     <div ng-view></div>      <script>         var app = angular.module('myApp', []);         var app = angular.module('myApp', ['ngRoute']);         app.config(function ($routeProvider) {             $routeProvider                 .when('/', {                     templateUrl: 'first.html',                     controller: 'FirstController'                 })                  .when('/second', {                     templateUrl: 'second.html',                     controller: 'SecondController'                 })                  .when('/third', {                     templateUrl: 'third.html',                     controller: 'ThirdController'                 })                  .otherwise({ redirectTo: '/' });         });          // Controller is a JS function that maintains          // application data and behavior using $scope object         // properties and methods can be attached to the         // $scope object inside a controller function          app.controller('FirstController', function ($scope) {             $scope.message = 'Hello from FirstController';         });         app.controller('SecondController', function ($scope) {             $scope.message = 'Hello from SecondController';         });         app.controller('ThirdController', function ($scope) {             $scope.message = 'Hello from ThirdController';         });     </script> </body>  </html> 

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20240806124856/spa.mp4


Next Article
Deployment of Angular Application using Github Pages

S

Shreyasi_Chakraborty
Improve
Article Tags :
  • AngularJS
  • Web Technologies
  • AngularJS-Questions

Similar Reads

  • How Single Page Applications work in Angular ?
    In traditional web applications, each time a user interacts with a website, the server sends a new HTML page back to the browser. This process can be slow and inefficient, resulting in a less-than-optimal user experience. Single Page Applications (SPAs) solve this problem by loading all necessary re
    7 min read
  • What is Single Page Application?
    A Single Page Application (SPA) is a type of web application that loads and updates content dynamically without refreshing the entire page. Unlike traditional websites, SPAs use modern technologies to enhance the user experience by minimizing interruptions and providing a smoother interface. Users c
    5 min read
  • Create Angular Application with SCSS
    Angular is a popular JavaScript framework for building web applications. It provides a set of tools and libraries for building client-side applications using modern web technologies. Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor that adds powerful features to CSS, such as variables
    6 min read
  • How to Build Single Page Applications?
    SPAs really revolutionized the way we think about and build web applications that are in tune with modernity. Compared to multi-page applications, SPAs offer better performance, finer transitions, and altogether a better user experience. Whether you are totally new to SPAs or aiming to make one for
    9 min read
  • Deployment of Angular Application using Github Pages
    There are various methods to deploy Angular Application such as Github Pages, Heroku, Firebase, etc. The Github provides the simplest way of all using the Github Pages. Steps to Create and Deploy Sample Angular Application to Github Pages: Install Node.js: a. Windows b. LinuxInstall Angular - CLICre
    2 min read
  • What are the main building blocks of an Angular Application?
    Angular is a widely used open-source front-end framework used to build dynamic web applications. It consists of several key building blocks that work together to create scalable, and maintainable applications. In this article, we will explore all the main building blocks of an Angular application an
    8 min read
  • What is View in AngularJS ?
    In AngularJS, the View is what the user can see on the webpage. In an application (as per the user’s choice), the chosen view is displayed. For this purpose, the ng-view directive is used, which allows the application to render the view. Any changes made in the view section of the application are re
    7 min read
  • What are some important practices to secure an Angular application ?
    In this article, we will see how we can secure our angular application from common types of vulnerabilities which can exploit the angular application. Also, we will see some best practices to protect such applications. Angular is a JavaScript development framework, which programmers extensively use
    5 min read
  • What is the role of ng-app, ng-init and ng-model directives in AngularJS ?
    In this article, we will learn about the directives in AngularJS & explore the roles of various directives such as ng-app, ng-init & ng-model in angularJS. The AngularJS directive is a command that gives HTML documents new functionality. When we use the AngularJS directives, it will first fi
    2 min read
  • How to Dockerize Angular Application
    Dockerizing an Angular application involves packaging it into a Docker container, which can simplify deployment and ensure consistency across different environments. Docker is a containerization platform that allows you to package applications and their dependencies into lightweight, portable contai
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences