Skip to main content

Introduction to Angular in Web Development: A Beginner's Guide to Building Modern Applications



Angular is a powerful and widely used front-end framework developed by Google. It is designed to create dynamic, scalable, and feature-rich single-page applications (SPAs). Since its inception, Angular has undergone significant changes, evolving into a comprehensive solution for web development.

In this article, we will explore:

  • What Angular is and why it is used

  • The architecture of Angular

  • Key features of Angular

  • Setting up an Angular project

  • Core Angular concepts (components, modules, services, routing, and directives)

  • Hands-on implementation with an example application


1. What is Angular?

Angular is a TypeScript-based open-source front-end framework used for building SPAs. It provides developers with tools to create interactive, modular, and maintainable web applications. Angular is backed by Google and has a large community of contributors, ensuring its continuous development and support.

Why Use Angular?

  • Component-based architecture: Reusable UI components make the application modular and scalable.

  • Two-way data binding: Syncs data between the model and view in real-time.

  • Dependency injection: Efficiently manages dependencies and improves application modularity.

  • Built-in routing: Provides seamless navigation between different views.

  • Directives and Pipes: Extend HTML functionalities and transform data efficiently.


2. Angular Architecture

Angular applications are structured into several core building blocks:

  • Modules (NgModules): The root module bootstraps the application and can include feature modules.

  • Components: Define the UI and logic of the application.

  • Templates and Views: The HTML structure of Angular components.

  • Directives: Extend the behavior of HTML elements.

  • Services and Dependency Injection: Facilitate data sharing and logic encapsulation.

  • Routing Module: Manages navigation within the application.


3. Setting Up an Angular Project

Before diving into an Angular application, let’s set up our environment.

Step 1: Install Node.js and Angular CLI

Ensure you have Node.js installed. Then, install the Angular CLI (Command Line Interface):

sh
npm install -g @angular/cli

Verify the installation:

sh
ng version

Step 2: Create a New Angular Project

Run the following command to create a new Angular project:

sh
ng new my-angular-app

Navigate into the project directory:

sh
cd my-angular-app

Step 3: Serve the Application

Run the development server:

sh
ng serve

Visit http://localhost:4200/ in your browser to see the default Angular app.


4. Core Angular Concepts

4.1 Components

Components are the building blocks of an Angular application. Each component consists of:

  • HTML Template: Defines the structure of the component.

  • TypeScript Class: Handles the logic.

  • CSS Styles: Defines the appearance.

Creating a Component

To create a new component, use the Angular CLI:

sh
ng generate component my-component

This creates four files:

  • my-component.component.ts (TypeScript logic)

  • my-component.component.html (HTML structure)

  • my-component.component.css (CSS styling)

  • my-component.component.spec.ts (Unit testing)

Example Component

ts
import { Component } from '@angular/core'; @Component({ selector: 'app-my-component', template: `<h2>Welcome to My Component!</h2>`, styles: [`h2 { color: blue; }`] }) export class MyComponent {}

To use this component, add <app-my-component></app-my-component> to app.component.html.


4.2 Modules (NgModules)

Angular applications are modular, and every application has at least one root module (AppModule).

Example of app.module.ts:

ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { MyComponent } from './my-component/my-component.component'; @NgModule({ declarations: [AppComponent, MyComponent], imports: [BrowserModule], providers: [], bootstrap: [AppComponent] }) export class AppModule { }

4.3 Services and Dependency Injection

Services allow data and logic to be shared across multiple components.

Creating a Service

Generate a service using Angular CLI:

sh
ng generate service data

Modify data.service.ts:

ts
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class DataService { getData() { return "Hello from Data Service!"; } }

Use the service in a component:

ts
import { Component, OnInit } from '@angular/core'; import { DataService } from '../data.service'; @Component({ selector: 'app-my-component', template: `<h2>{{ message }}</h2>` }) export class MyComponent implements OnInit { message: string = ''; constructor(private dataService: DataService) {} ngOnInit() { this.message = this.dataService.getData(); } }

4.4 Routing in Angular

Routing enables navigation between different components.

Setting Up Routing

Modify app-routing.module.ts:

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

Add a navigation link in app.component.html:

html
<a routerLink="/my-component">Go to My Component</a> <router-outlet></router-outlet>

5. Building a Simple Angular Application

Let's build a basic To-Do List App.

Step 1: Create a To-Do Component

sh
ng generate component todo

Step 2: Modify todo.component.ts

ts
import { Component } from '@angular/core'; @Component({ selector: 'app-todo', templateUrl: './todo.component.html', styleUrls: ['./todo.component.css'] }) export class TodoComponent { tasks: string[] = []; newTask: string = ''; addTask() { if (this.newTask.trim()) { this.tasks.push(this.newTask); this.newTask = ''; } } removeTask(index: number) { this.tasks.splice(index, 1); } }

Step 3: Modify todo.component.html

html
<div> <h2>To-Do List</h2> <input [(ngModel)]="newTask" placeholder="Enter task"> <button (click)="addTask()">Add</button> <ul> <li *ngFor="let task of tasks; let i = index"> {{ task }} <button (click)="removeTask(i)">Remove</button> </li> </ul> </div>

Step 4: Update app.module.ts

Import FormsModule to enable two-way data binding:

ts
import { FormsModule } from '@angular/forms'; @NgModule({ imports: [FormsModule] })

Conclusion

Angular is a robust framework that simplifies the development of dynamic web applications. By understanding its core concepts—components, modules, services, routing, and directives—you can build scalable and maintainable applications.

We built a simple To-Do List App using Angular's component-based structure, two-way data binding, and event handling.

Next Steps

  • Learn more about State Management (NgRx)

  • Explore RxJS for reactive programming

  • Deploy your Angular app using Firebase or Vercel