Open In App

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 resources and content into a single page, allowing for faster and more responsive interactions.

A Single Page Application is a web application that loads a single HTML page and dynamically updates the content as the user interacts with the application. Instead of requesting a new page from the server each time the user interacts with the application, a SPA loads all necessary resources and content up front and uses client-side rendering to update the page dynamically. SPAs are important because they provide a faster and more responsive user experience compared to traditional web applications. By loading all necessary resources up front, SPAs reduce the amount of time it takes for pages to load and content to appear on the screen. SPAs also allow for more seamless interactions between the user and the application, resulting in a more natural and intuitive user experience. The design pattern used in SPAs is based on client-side rendering, where the majority of the application logic and rendering occurs in the user’s browser instead of on the server. This allows for faster and more efficient processing of user interactions, resulting in a smoother user experience.



Working of SPAs: The working of a SPA involves the following steps:

 



Some of the Main Components used in a SPA:

For a detailed description of all the components, refer to the Angular 7 Architecture article.

Example 1: The below example illustrates the approach to implement & the working of the Single Page Applications in Angular.

npm install -g @angular/cli
ng new spa

This will create a new Angular project in a directory named “spa”.

cd spa
ng g c home

This will create a new component named “home” in the “src/app” directory.




<h1 style="color: green;">
    Welcome to my GFG APP!
</h1>
<h3>
    This explains how Single Page 
    Application works in Angular.
</h3>
<router-outlet></router-outlet>




import { NgModule } from '@angular/core';
import { Routes, RouterModule } 
    from '@angular/router';
import { HomeComponent } 
    from './home/home.component';
  
const routes: Routes = [
    { path: '', component: HomeComponent }
];
  
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }




<h4>This is the home page.</h4>




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } 
    from './app-routing.module';
import { AppComponent } 
    from './app.component';
import { HomeComponent } 
    from './home/home.component';
  
@NgModule({
    declarations: [
        AppComponent,
        HomeComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        FormsModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

ng serve -o

This will start the development server and launch the application in your default web browser.

Output:

 

Example 2: This example demonstrates how Single Page Applications work in Angular by showing how changes to the state of the items array are automatically reflected in the view without the need for a page refresh.

npm install -g @angular/cli
ng new my-spa-app

This will create a new Angular project with the name my-spa-app.

cd my-spa-app
ng generate component item-list

This will create a new component with the name item-list.




<h2>Items</h2>
<ul>
    <li *ngFor="let item of items">{{ item }}</li>
</ul>

This code displays a <h2> tag with the text “Items” and a list of items using the *ngFor directive.




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-item-list',
    templateUrl: './item-list.component.html',
    styleUrls: ['./item-list.component.css']
})
export class ItemListComponent {
    items = ['Item 1', 'Item 2', 'Item 3'];
}

This code creates an array of items and assigns it to the items variable.




<h1 style="color: green;">
    Welcome to my GFG APP!
</h1>
<h3>
    This explains how Single Page 
    Application works in Angular.
</h3>
<app-item-list></app-item-list>

This code loads the item-list component in the app.component.html file.

ng serve --open 

This will compile the app and open it in your default browser at http://localhost:4200/.




import { Component } from '@angular/core';
  
@Component( {
    selector: 'app-item-list',
    templateUrl: './item-list.component.html',
    styleUrls: [ './item-list.component.css' ]
} )
export class ItemListComponent {
    items = [ 'Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5' ];
}

This code adds two more items to the items array.




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { AppRoutingModule }
    from './app-routing.module';
import { AppComponent } 
    from './app.component';
import { ItemListComponent }
    from './item-list/item-list.component';
  
@NgModule({
    declarations: [
        AppComponent,
        ItemListComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

Output:

 


Article Tags :