Angular Basics - an Introduction
Published on: Mar 9th, 2025
What is Angular?
Angular is a TypeScript-based web framework developed by Google. It is used to build dynamic, single-page web applications (SPAs) and is one of the most popular front-end frameworks in the industry. Unlike React, which is primarily a UI library, Angular is a full-fledged framework that provides built-in features such as dependency injection, two-way data binding, and routing.
Many enterprises prefer Angular because it enforces a structured architecture, making it an excellent choice for large-scale applications. Angular is also known for its performance and scalability, making it ideal for building complex web applications that require real-time updates and interactive user interfaces.
Why Use Angular?
Angular offers several key features that make it a popular choice for web development:
- Two-way data binding: Angular synchronizes the model and the view in real-time, allowing changes in one to reflect in the other automatically.
- Dependency injection: Angular's built-in dependency injection system makes it easy to manage dependencies and promote code reusability.
- Routing: Angular's router module allows developers to create multiple views and navigate between them seamlessly.
- Forms: Angular provides powerful form-handling capabilities, including form validation and error handling.
- Directives: Angular's directives allow developers to extend HTML with custom attributes and tags, making it easier to create reusable components.
Setting Up Your First Angular Project
To get started with Angular, the first thing you need to do is install the
Node.js
installed by visiting this link Node.js Download, you then want to open
your IDE, I personally use Visual Studio Code
Visual Studio Code Download but
you can use any
that supports the use of Node after
you open your IDE open an terminal within the file folder and enter in this code npm
install -g @angular/cli
this will install the Angular CLI globally on your machine.
after you have installed the Angular CLI you can create a new Angular project by running the
following command in your terminal ng new my-app
this will create a new Angular
project called my-app
in the current directory. You can then navigate into the
project folder and start the development server by running cd my-app
and then
ng serve
this will start the development server and you can view your Angular
application by visiting http://localhost:4200
in your browser.
Understanding Angular Components
In Angular, components are the building blocks of an application. A component is a TypeScript
class
that interacts with the HTML template and manages the application's data and behavior, to
generate a component you can use
this command ng generate component my-component
Each
component consists of three main parts:
- Typescript File: The TypeScript class that defines the component's properties and methods.
- HTML Template: The HTML template that defines the component's view.
- CSS File: The CSS file that styles the component
Here's an example of a simple Angular component:
The TypeScript class:
import { Component } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: './my-component.html',
styleUrls: ['./my-component.css']
})
export class MyComponent {
title = 'My Example App';
}
The CSS file:
h1 {
color: blue;
}
The HTML template:
<h1>{{ title }}</h1>
This component displays a title using an h1
tag with the value of the
title
property, in order to use this component in your application you would add
the following code to your app.component.html
file
<my-component></my-component>
Data binding in Angular
Data binding is a powerful feature in Angular that allows you to synchronize data between the component class and the HTML template. There are four types of data binding in Angular:
- Interpolation: Interpolation allows you to display component properties in
the
HTML template using double curly braces
{{ }}
. For example,{{ title }}
will display the value of thetitle
property in the template. - Property binding: Property binding allows you to set an element's property
to
the value of a component property. For example,
<img [src]="imageUrl">
will set thesrc
attribute of theimg
tag to the value of theimageUrl
property. - Event binding: Event binding allows you to listen for events in the HTML
template and call a method in the component class. For example,
<button (click)="onClick()">Click me</button>
will call theonClick()
method when the button is clicked. - Two-way data binding: Two-way data binding allows you to bind a component
property to an input element and update the property when the input value changes. For
example,
<input [(ngModel)]="name">
will bind thename
property to the input element and update the property when the input value changes.
Handling User Input with Events
Angular provides event binding to handle user input in the HTML template. You can bind to
standard
DOM events such as click
, mouseover
, and keyup
, as well
as
custom events emitted by components. Here's an example of event binding in Angular:
The TypeScript class:
import { Component } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: './my-component.html',
styleUrls: ['./my-component.css']
})
export class MyComponent {
title = 'My Example App';
onClick(){
alert('Button clicked!');
}
}
The HTML template:
<button (click)="onClick()">Click me</button>
When the button is clicked, the onClick()
method in the component class will be
called, and the message 'Button clicked!'
will display an alert.
Using the HTTPClient
Angular provides the HttpClient
module to make HTTP requests to a server. You can use
the HttpClient
module to fetch data from a REST API, send data to a server, and
handle responses. Here's an example of how to use the HttpClient
module in Angular:
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'my-component',
templateUrl: './my-component.html',
styleUrls: ['./my-component.css']
})
export class MyComponent
{
constructor(private http: HttpClient) {}
getData() {
this.http.get('https://api.example.com/data')
.subscribe((data: any) => {
console.log(data);
});
}
}
In this example, we inject the HttpClient
service into the component's constructor
and use it to make a GET request to the https://api.example.com/data
endpoint. We
then subscribe to the response and log the data to the console.
Routing in Angular
Routing is an essential feature in Angular that allows you to navigate between different views in
your application. Angular's router module provides a powerful and flexible way to define routes
and handle navigation, in your app-routing-module.ts
you can define your routes
like
so:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
Here, we define two routes: ''
for the home component and 'about'
for
the
about component. We then import the RouterModule
and define the routes in the
AppRoutingModule
class. Finally, we export the RouterModule
so it can
be
used in other modules.
To navigate between routes in your application, you can use the routerLink
directive
in your HTML template. For example, <a routerLink="/about">About</a>
will
navigate to the about
route when the link is clicked.
In Conclusion
Angular is a powerful front-end framework that provides a robust set of features for building dynamic web applications. By understanding the basics of Angular components, data binding, event handling, and routing, you can create modern and interactive web applications with ease. Whether you're a beginner or an experienced developer, Angular offers a comprehensive toolkit to help you build scalable and maintainable applications.