Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

Angular Framework Guide

Learn Angular - TypeScript-based web framework for building large-scale applications. Master components, services, routing, and more.

jiocoder TeamUpdated Jun 6, 20263 min readAngular
Angular Framework Guide

What is Angular?#

Angular is a platform and framework for building single-page client applications using HTML and TypeScript. Angular is written in TypeScript and implements core and optional functionality as a set of TypeScript libraries that you import into your applications.

Angular was developed by Google and is a complete rewrite of AngularJS. It's designed for building large-scale, enterprise applications with a focus on maintainability, testability, and developer productivity.

Key Features#

  • Components - Building blocks of Angular applications
  • Services - Reusable business logic
  • Dependency Injection - Built-in DI system
  • Routing - Client-side routing
  • Forms - Template-driven and reactive forms
  • HTTP Client - Built-in HTTP service
  • Testing - First-class testing support

Installation#

Install Angular CLI globally:

Code
# Install Angular CLI
npm install -g @angular/cli

# Create new project
ng new my-angular-app

# Navigate to project
cd my-angular-app

# Start development server
ng serve

# Build for production
ng build --prod

Project Structure#

Code
my-angular-app/
  ├── src/
  │   ├── app/
  │   │   ├── components/
  │   │   ├── services/
  │   │   ├── app.component.ts
  │   │   └── app.module.ts
  │   ├── assets/
  │   ├── environments/
  │   └── main.ts
  ├── angular.json
  └── package.json

Component#

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

@Component({
  selector: 'app-root',
  template: `
    <h1>{{ title }}</h1>
    <p>Count: {{ count }}</p>
    <button (click)="increment()">Increment</button>
    <button (click)="decrement()">Decrement</button>
  `,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'My Angular App';
  count = 0;
  
  increment() {
    this.count++;
  }
  
  decrement() {
    this.count--;
  }
}

Services#

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

@Injectable({
  providedIn: 'root'
})
export class DataService {
  private data: any[] = [];
  
  getData() {
    return this.data;
  }
  
  addItem(item: any) {
    this.data.push(item);
  }
  
  removeItem(index: number) {
    this.data.splice(index, 1);
  }
}

// Usage in component
import { Component } from '@angular/core';
import { DataService } from './data.service';

@Component({
  selector: 'app-data',
  template: '<div>{{ data | json }}</div>'
})
export class DataComponent {
  data: any[] = [];
  
  constructor(private dataService: DataService) {
    this.data = this.dataService.getData();
  }
}

Routing#

Code
// app-routing.module.ts
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 },
  { path: 'user/:id', component: UserComponent },
  { path: '**', redirectTo: '' }
];

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

// Template
<router-outlet></router-outlet>
<a routerLink="/about">About</a>

HTTP Client#

Code
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  private apiUrl = 'https://api.example.com';
  
  constructor(private http: HttpClient) {}
  
  getUsers(): Observable<User[]> {
    return this.http.get<User[]>(`${this.apiUrl}/users`);
  }
  
  createUser(user: User): Observable<User> {
    return this.http.post<User>(`${this.apiUrl}/users`, user);
  }
}

Forms#

Code
// Template-driven form
<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)">
  <input name="name" ngModel required>
  <input name="email" ngModel type="email" required>
  <button type="submit" [disabled]="!userForm.valid">Submit</button>
</form>

// Reactive form
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

export class UserFormComponent {
  userForm: FormGroup;
  
  constructor(private fb: FormBuilder) {
    this.userForm = this.fb.group({
      name: ['', Validators.required],
      email: ['', [Validators.required, Validators.email]]
    });
  }
  
  onSubmit() {
    if (this.userForm.valid) {
      console.log(this.userForm.value);
    }
  }
}

Best Practices#

  • Use TypeScript for type safety
  • Follow Angular style guide
  • Use services for shared logic
  • Implement lazy loading for routes
  • Use OnPush change detection for performance
  • Follow single responsibility principle
  • Write unit tests for components and services

Angular is a comprehensive framework perfect for building large-scale enterprise applications. Its opinionated structure and powerful features make it an excellent choice for teams building complex applications.

Article discussion

(0)

Join the discussion — login to comment and help other developers.

Login to comment