Swiftorial Logo
Home
Swift Lessons
Tutorials
Career
Resources

Angular Fundamentals

1. Introduction

Angular is a platform and framework for building single-page client applications using HTML and TypeScript. Angular is written in TypeScript and is maintained by Google.

2. Core Concepts

Key Concepts

  • Components - The building blocks of an Angular application.
  • Modules - Containers for a cohesive block of code dedicated to an application domain, a workflow, or a closely related set of capabilities.
  • Templates - Define the HTML view and can include Angular markup.
  • Services - Used to encapsulate business logic and data.
  • Dependency Injection - A design pattern used to implement IoC (Inversion of Control).

3. Installation

To start using Angular, you need to install Node.js and Angular CLI. Follow these steps:

  1. Install Node.js from nodejs.org.
  2. Open your command line interface.
  3. Run the command: npm install -g @angular/cli to install Angular CLI globally.
  4. Create a new Angular application: ng new my-app.
  5. Navigate to the project directory: cd my-app.
  6. Run the application: ng serve and open http://localhost:4200 in your browser.

4. Component Architecture

Angular applications are built using a component-based architecture. Each component encapsulates the view defined in a template, and the logic defined in a class. Here’s a simple example:


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

@Component({
  selector: 'app-hello-world',
  template: '

Hello, World!

' }) export class HelloWorldComponent { }

5. Data Binding

Angular supports various types of data binding:

  • Interpolation: Binding data from the component to the template using curly braces.
  • Property Binding: Binding data to the properties of HTML elements.
  • Event Binding: Binding events to methods in the component.
  • Two-way Data Binding: A combination of property and event binding using the [(ngModel)] directive.

6. Routing

Angular Router enables navigation between different views or components. To set up routing, follow these steps:

  1. Import RouterModule in your app module.
  2. Define routes in an array.
  3. Use the <router-outlet> directive to mark where the routed components should be displayed.

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];

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

7. Services

Services in Angular are used to encapsulate data and business logic. You can create a service using the Angular CLI:


ng generate service data
            

Then inject the service into components:


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

@Injectable({
  providedIn: 'root',
})
export class DataService {
  getData() {
    return [1, 2, 3];
  }
}
            

8. Best Practices

To ensure your Angular application is efficient and maintainable, follow these best practices:

  • Use Angular CLI for project scaffolding.
  • Organize code into modules.
  • Utilize services for business logic.
  • Keep components simple and focused.
  • Implement lazy loading for feature modules.

9. FAQ

What is Angular?

Angular is a platform for building mobile and desktop web applications.

What language is Angular written in?

Angular is primarily written in TypeScript.

Can Angular be used for mobile development?

Yes, Angular can be used for mobile development, especially with frameworks like Ionic.