Explain the Architecture Overview of Angular ? (original) (raw)

Last Updated : 24 Apr, 2025

Angular is a client-side front-end framework developed by a team of developers at Google based on Typescript. It is used for building dynamic and single-page web applications (SPAs). Also, Angular is a versatile framework for building web applications and offers a wide range of features and tools to streamline the development process and create robust and maintainable applications.

Angular Architecture Overview

To develop any web application, Angular follows the MVC (Model-View-Controller) and MVVM (Model-View-ViewModel) design patterns, which facilitates a structured and organized approach to designing the application, along with making it easier to manage code, improve maintainability, & etc. These types of design patterns usually maintain a clear distinction between data (Model), user interface (View), and logic (Controller/ViewModel), which results in more scalable and testable applications. It also provides code reusability and ensures a smoother development process for large & complex projects.

archi

Angular Architecture

An Angular Application contains the following building blocks:

Table of Content

We will explore the above topics & will understand their basic syntaxes.

Modules

Example: app.module.ts

JavaScript `

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component';

@NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }

`

Components

Example: app.component.ts

JavaScript `

import { Component } from '@angular/core';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'AngularApp'; }

`

Templates

Example: app.component.html

HTML `

Welcome to GeeksForGeeks

Angular Architecture consists of :

`

Directives

Example:

ngSwitch Example

Input string :
One
Two
Three
Four
Five
This is Default

ngFor and ngIf Example

{{ emp.name }}: {{ emp.age }}

ngStyle Example

      <!-- Custom Directive: ng g directive uppercase -->
    <div>
        <input type="text" appUppercase 
               placeholder="Enter text" />
    </div>
</div>

`

import { Component } from '@angular/core';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'AngularApp'; num: number = 0; employee: Employee[] = [

    { name: 'Emp1', age: 30 },
    { name: 'Emp2', age: 37 },
    { name: 'Emp3', age: 26 },

]

} class Employee {

name: string;
age: number;

}

`

import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';

@Directive({ selector: '[appUppercase]' }) export class UppercaseDirective {

constructor(private el: ElementRef, 
            private renderer: Renderer2) { }

@HostListener('input', ['$event']) onInputChange(event: Event) {
    const inputValue = 
        (event.target as HTMLInputElement).value;
    const newValue = 
        inputValue.toUpperCase();
    this.renderer.setProperty(this.el.nativeElement, 'value', newValue);
}

}

`

Services

ng generate service counter

Example: counter.service.ts

JavaScript `

import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root', }) export class CounterService { private count = 0;

increment(): void {
    this.count++;
}

getCount(): number {
    return this.count;
}

}

`

Dependency Injection(DI)

Router

Example: app-routing.module.ts

JavaScript `

import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { EmployeeListComponent } from './employee-list/employee-list.component'; import { CreateEmployeeComponent } from './create-employee/create-employee.component'; import { UpdateEmployeeComponent } from './update-employee/update-employee.component'; import { EmployeeDetailsComponent } from './employee-details/employee-details.component';

const routes: Routes = [ { path: 'employees', component: EmployeeListComponent }, { path: 'create-employee', component: CreateEmployeeComponent }, { path: '', redirectTo: 'employees', pathMatch: 'full' }, { path: 'update-employee/:id', component: UpdateEmployeeComponent }, { path: 'employee-details/:id', component: EmployeeDetailsComponent } ];

@NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }

`

State Management

HTTP Client

Example: post-list.component.ts

JavaScript `

import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http';

@Component({ selector: 'app-post-list', templateUrl: './post-list.component.html', styleUrls: ['./post-list.component.css'], }) export class PostListComponent implements OnInit { posts: any[] = [];

constructor(private http: HttpClient) { }

ngOnInit(): void {
    this.http
        .get < any[] > (
        'https://jsonplaceholder.typicode.com/posts')
            .subscribe((data) => {
                this.posts = data;
            });
}

}

`