Angular router tutorial

Angular Router Setup: Your Blueprint for Smooth Navigation in Single-Page Apps

Imagine a website where every click necessitated a reload. Frustrating, isn’t it? Single-page apps can ensure such frustration doesn't come to life. And in this seamless navigation model, Angular Router is the secret weapon. Whether you are building an e-commerce platform or a portfolio site, Angular Router lets you create fast, dynamic user experiences without full page refreshes.

You will learn in this guide how to:

  • Configure an Angular Router from scratch
  • Employ lazy loading to get better performance
  • Use Guards to protect routes
  • Choose sensible detail information and warning sounds that are both scalable and maintainable

Let us make your Angular app a well-connected navigation powerhouse!

What Is Angular Router and Why Does It Matter?

Angular Router is a powerful library that maps URLs to components so that users can flip between views without reloading the page. It’s indispensable for:

  • SPA efficiency: Faster transitions between pages
  • Bookmarkable URLs: Users can save or share particular views
  • Modular design: Organizing code into feature modules

Optimization Tidbit: When optimized correctly, HTTP Archive says SPAs (single-page applications) actually load 30-50% faster than traditional multi-page sites in terms of initial loading times.

Step 1: Installing Angular Router

If you’re beginning a new project, routing comes baked into Angular CLI by default. For existing projects, you can add it like so:

ng generate module app-routing --flat --module=app  

This creates an app-routing.module.ts file and imports it into your AppModule.

Step 2: Basic Route Configuration

Define your routes in 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';  
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';  

const routes: Routes = [  
  { path: '', component: HomeComponent },  
  { path: 'about', component: AboutComponent },  
  { path: '**', component: PageNotFoundComponent } // Wildcard route for 404  
];  

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

Main Points for Summary:

  • path: '' sets the default route (e.g., / redirects t0 HomeComponent).
  • path: '**' catches unmatched URLs (e.g., /invalid-route).

Step 3: Adding Navigation with Router Links

Use routerLink directives in your template instead of <a href> to avoid full-page reloads.

<nav>  
  <a routerLink="/" routerLinkActive="active">Home</a>  
  <a routerLink="/about" routerLinkActive="active">About</a>  
</nav>  

Pro Tip: routerLinkActive="active" adds a CSS class to the active link for styling.

Step 4: Dynamic Routes with Parameters

Grab URL parameters (e.g., product IDs) by means of /:id:

const routes: Routes = [  
  { path: 'products/:id', component: ProductDetailComponent }  
];  

Access the Parameter in Your Component:

import { ActivatedRoute } from '@angular/router';  
export class ProductDetailComponent {  
  constructor(private route: ActivatedRoute) {  
    this.route.params.subscribe(params => {  
      console.log('Product ID:', params['id']);  
    });  
  }  
}  

Step 5: Lazy Loading for Faster Load Times

Lazy loading pushes the loading of modules back until later, so that initial bundle size can be small.

  1. Create a Feature Module:
ng generate module products --route products --module app  
  1. Update Routes:
const routes: Routes = [  
  { path: 'products', loadChildren: () => import('./products/products.module').then(m => m.ProductsModule) }  
];  

This ensures that the ProductsModule is only loaded if the user navigates to /products.

Step 6: Protecting Routes with Guards

Guards are used to restrict access (e.g., ensure that a user is authenticated).

  1. Make an Auth Guard:
import { Injectable } from '@angular/core';  
import { CanActivate, Router } from '@angular/router';  

@Injectable({ providedIn: 'root' })  
export class AuthGuard implements CanActivate {  
  constructor(private router: Router) {}  
  canActivate(): boolean {  
    const isLoggedIn = false; // Look up the user's auth status  
    if (!isLoggedIn) this.router.navigate(['/login']);  
    return isLoggedIn;  
  }  
}  
  1. Apply the Guard to Routes:
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] }  

Angular Router Best Practices

  1. Organize Routes Logically: Group related routes into feature modules.
  2. Use Lazy Loading: Prioritize performance.
  3. Avoid Hardcoding URLs: Use named constants for paths.
  4. Handle 404s: Always include a wildcard route.

Navigate Your Angular App Like a Pro

Angular Router gives your app the feel of being seamless and smooth. By mastering route configuration, lazy loading modules, and guards, you’ll create SPAs that are responsive and fast.


With this guide, you can structure the navigation in your app to be logical and high-performance. Happy routing!

Mastering Reactive Forms in Angular

If you are attempting to build a sign-up form that changes according to user input, such as supplying additional fields when someone selects “Business Account,” in addition to real-time password validation, it would be difficult. Not with Angular reactive forms!

Reactive forms afford developers superior control over form logic, validation, and dynamic behavior. For complex apps, they are indeed the best. Whether building a multi-step checkout flow or conducting a survey, reactive forms are your friend.

By the time you finish this post, you will know how to:

  • Build reactive forms with clean, maintainable code.
  • Add dynamic form fields and custom validation.
  • Handle complex situations such as nested forms and cross-field validation.
  • Avoid common errors

Ready to enhance your skills with Angular? Let’s start!

What Exactly Are Reactive Forms in Angular?

Reactive forms (also known as "model-driven" forms) in Angular are a way of programmatically handling forms. Unlike template-driven forms, which are directed by HTML tags, reactive forms use TypeScript classes like FormGroup, FormControl, and FormArray to manage form logic entirely. This method gives us unparalleled flexibility in creating dynamic and scalable forms.

Key Advantages of Reactive Forms

  • Explicit Control: Code your form structure and validation rules.
  • Dynamic Behavior: Programmatically add or remove fields in a form.
  • Reactive Programming: Take advantage of tools like valueChanges for real-time updates.
  • Complex Validation: Support for cross-field validation and custom rules.

When Should You Use Reactive Forms Over Template-Driven Forms?

Feature Reactive Forms Template-Driven Forms
Complexity Best for complex forms Ideal for simple forms
Control Programmatic (TypeScript) Declarative (HTML)
Validation Custom and dynamic rules HTML5 and basic validators
Testing Easier for unit testing Requires DOM testing

Pro Tip: According to the 2023 Stack Overflow Survey, 68% of Angular developers choose reactive forms for enterprise applications due to their scalability.

Build Your First Reactive Form: A Step-by-Step Guide

Here, we’ll set up a user registration form with dynamic password validation.

Step 1: Import ReactiveFormsModule

In your AppModule, import ReactiveFormsModule:

import { ReactiveFormsModule } from '@angular/forms';  
@NgModule({  
  imports: [ReactiveFormsModule],  
})  
export class AppModule {}  

Step 2: Define Form Structure in Component

Use FormBuilder to simplify form creation:

import { FormBuilder, Validators } from '@angular/forms';  
export class RegistrationComponent {  
  constructor(private fb: FormBuilder) {}  
  registrationForm = this.fb.group({  
    email: ['', [Validators.required, Validators.email]],  
    password: ['', [  
      Validators.required,  
      Validators.minLength(8),  
      Validators.pattern(/^(?=.*[A-Z])(?=.*\d).+$/)  
    ]],  
    newsletter: [false]  
  });  

  onSubmit() {  
    if (this.registrationForm.valid) {  
      console.log('Form Data:', this.registrationForm.value);  
    }  
  }  
}  

Step 3: Bind Template to the Form

<form [formGroup]="registrationForm" (ngSubmit)="onSubmit()">  
  <label>Email: <input formControlName="email" /></label>  
  <label>Password: <input type="password" formControlName="password" /></label>  
  <label>Subscribe to Newsletter? <input type="checkbox" formControlName="newsletter" /></label>  
  <button type="submit">Sign Up</button>  
</form>  

Add Real-Time Validation and Error Messages

Use FormControl to display friendly, human-readable error messages:

<div *ngIf="registrationForm.get('email').hasError('required')">  
  Email is required.  
</div>  
<div *ngIf="registrationForm.get('email').hasError('email')">  
  Invalid email format.  
</div>  

Pro Tip: To safely access individual controls, use registrationForm.get('fieldName').

Handling Dynamic Form Fields with FormArray

Want users to enter multiple email addresses? Use FormArray:

Component:

emails = this.fb.array([this.fb.control('')]);  
addEmail() {  
  this.emails.push(this.fb.control(''));  
}  

Template:

<div formArrayName="emails">  
  <div *ngFor="let email of emails.controls; let i = index">  
    <input [formControlName]="i" />  
  </div>  
</div>  
<button (click)="addEmail()">Add Email</button>  

Reactive Forms: Pros and Cons

👍 Advantages

  • Complete Control: Best for complex workflows.
  • Reactive Approach: Uses RxJS for features like debouncing inputs.
  • Testing Made Easy: Logic is in TypeScript, making unit tests easier.

👎 Disadvantages

  • Steep Learning Curve: Requires mastery of FormControl and FormGroup.
  • More Boilerplate: More setup than template-driven forms.

Case Study: How Company X Improved Their SaaS Business

A small fintech startup transformed into a top brand by switching from template-driven to reactive forms.

Outcomes:

  • 40% fewer submission errors.
  • 25% faster response time.

Conclusion: Build Robust Angular Forms

By mastering FormGroup, FormControl, and FormArray, you can create powerful, scalable forms.

Your Next Steps:

  1. Experiment with the form project above.
  2. Add a custom validator (e.g., Confirm Password).

Found This Guide Useful? Share it with your friends 

Angular Template-Driven Forms

Template-Driven Forms in Angular.

template driven forms in angular


What Are Template-Driven Forms in Angular Exactly?

  • Two-Way Data Binding: Use ngModel to bind data between typescript and html.
  • Automatic Form Control: Angular is creating FormControl instances by default unless you specify otherwise.
  • Built-in Validation: we can use HTML5 validation attributes (required, email) together with Angular's custom validators.

Your First Template-Driven Form from Scratch, Step by Step

Lets design a simple login form using template driven forms in angular

Step 1: Set Up the Angular Module

Make sure your AppModule includes FormsModule:

template driven forms angular

Step 2: Create the Template for the Form

template driven forms angular html form

Step 3: Handle Form Submission in Your Component

onSubmit(form: NgForm) {  
  if (form.valid) {  
    console.log('Form Data:', form.value);  
    // Send data to your backend here  
  }  
}  

Explanation:

  • #loginForm="ngForm" provides a reference to the form's NgForm instance.
  • ngModel binds input values with your component.
  • (ngSubmit) is triggered when the user submits the form.

Adding Validation and Error Messages

Validation is vital to ensure that our input is a legal and meaningful data point. Here's how to present user-friendly error messages:

<div *ngIf="email.invalid && email.touched">  
  <span *ngIf="email.errors?.required">Email required.</span>  
  <span *ngIf="email.errors?.email">Incorrect format for email.</span>  
</div>  

<div *ngIf="password.invalid && password.touched">  
  <span *ngIf="password.errors?.required">Password is required.</span>  
  <span *ngIf="password.errors?.minlength">Password should contain at least 6 characters.</span>  
</div>  

Advantages and Disadvantages of Template-Driven Forms

Advantages:

  • Easy Setup: Perfect for simple forms.
  • Less Boilerplate: No need to manually declare FormControls.
  • Familiar Syntax: Uses HTML and data binding, suitable for beginners.

Disadvantages:

  • Limited Control: Difficult to perform things such as complex form management or dynamic validation with this model.
  • Testing Challenges: The logic is in the template which makes unit tests harder.

Tips for Writing Template-Driven Forms

  1. Keep It Simple: Template-driven forms are best for simple use cases like logins, and contact forms.
  2. Use Built-in Validators: Combine custom Angular validators with HTML5 attributes for the strongest checks.
  3. Organize Code Structure: Place complex logic in components where it makes sense.
  4. Use NgModelGroup for Grouping: Group related fields such as billing and shipping addresses together.

Real time scenario: 

Create template driven form for user object which we will get from an api or from mock data:

1. Create user service class which will have api logic of getting user objects 
  i am providing sample syntax but you can use online mock data apis for this

template driven forms angular user service

2. Component logic: call user api from component and get data:
  • Now we have a service with getuser and updateuser methods which will call apis and get data.
  • we need to call getUser method and subscribe to it ,
  • we will capture the response and store in our user object and use same object in html to populate data in form
template driven forms in angular component

3. Template driven form in html
  • Do not forget to add name attribute while using ngModel  for template driven forms.
  • Try to practice all the validations , here i haven given some sample for your reference.
template driven forms angular html page

How to pass data between components using routes in Angular?

Techniques to Share Routes Data in Angular—How Efficient Communication Works Among Components of an Angular Application

In the world of Angular applications, components often need to share data, especially as they move from view to view. While services and state management libraries such as NgRx are often the answer, Angular's routing provides some lightweight techniques for passing information directly along routes.

If you're building a product detail page, search filter, multi-step form, or something else entirely, you need to learn how to transfer or pass data through routes. This guide demonstrates several methods with practical examples and advice on best practices.

How to Pass Data Between Components Using Routes in Angular

1. The Need to Share Data via Routes

  • Stateless Navigation: Prevent simple data transfer from becoming overly dependent (and inducing spaghetti code in your otherwise clean services or components).
  • Bookmarkable URLs: Keeping data in the URL ensures users can return to their entry anytime without any problem.
  • Lightweight: Designed for tiny, transient pieces of information like IDs and filters.

2. Passing Data via Routes

Route Parameters

Use Case: Passing key data, such as an ID (e.g., /products/123).

Implementation

Define the Route:

// app-routing.module.ts
const routes: Routes = [
  { path: 'product/:id', component: ProductDetailComponent }
];

Navigate with the Parameter:

// product-list.component.ts
navigateToProduct(id: number) {
  this.router.navigate(['/product', id]);
}

Retrieve the Parameter:

// product-detail.component.ts
import { ActivatedRoute } from '@angular/router';

export class ProductDetailComponent {
  constructor(private route: ActivatedRoute) {
    this.route.paramMap.subscribe(params => {
      const id = params.get('id');
      // Fetch product details using the ID
    });
  }
}

Query Parameters

Use Case: Passing optional data such as filters or sorting options (e.g., /products?category=books).

Implementation

Navigate with Query Params:

// product-list.component.ts
applyFilter(category: string) {
  this.router.navigate(['/products'], { queryParams: { category: category } });
}

Retrieve the Query Parameter:

// product-list.component.ts
this.route.queryParamMap.subscribe(params => {
  const category = params.get('category');
  // Filter products according to category
});

Route Data Property

Use Case: Passing static or resolved data (e.g., page titles, permissions).

Using Data in Route Configuration

Define Static Data:

// app-routing.module.ts
{
  path: 'dashboard',
  component: DashboardComponent,
  data: { requiresAuth: true, title: 'User Dashboard' }
}

Access the Data:

// dashboard.component.ts
ngOnInit() {
  this.route.data.subscribe(data => {
    console.log(data.title); // Output: "User Dashboard"
  });
}

Dynamic Data with Resolvers

Create a Resolver:

// product.resolver.ts
@Injectable({ providedIn: 'root' })
export class ProductResolver implements Resolve {
  constructor(private productService: ProductService) {}

  resolve(route: ActivatedRouteSnapshot) {
    return this.productService.getProduct(route.params['id']);
  }
}

Configure the Route with Resolver:

// app-routing.module.ts
{
  path: 'product/:id',
  component: ProductDetailComponent,
  resolve: { product: ProductResolver }
}

Retrieve the Resolved Data:

// product-detail.component.ts
ngOnInit() {
  this.route.data.subscribe(data => {
    this.product = data.product;
  });
}

State Object (NavigationExtras)

Use Case: Passing temporary or sensitive data without putting it in the URL.

Implementation

Navigate with State:

// checkout.component.ts
proceedToPayment() {
  this.router.navigate(['/payment'], { state: { cartItems: this.cartItems } });
}

Retrieve the State:

// payment.component.ts
ngOnInit() {
  this.cartItems = history.state.cartItems;
}

Practical Example: User Profile Editor

Scenario

Pass a user ID via route parameters and use a resolver to retrieve user data.

Route Configuration

{
  path: 'profile/:userID',
  component: ProfileComponent,
  resolve: { user: UserResolver }
}

Create Resolver:

// user.resolver.ts
resolve(route: ActivatedRouteSnapshot) {
  return this.userService.getUser(route.params['userId']);
}

Retrieve Data in Component:

// profile.component.ts
ngOnInit() {
  this.route.data.subscribe(data => {
    this.user = data.user;
  });
}

Best Practices

  • Use Route Parameters for Necessary Data: Keep URLs neat and meaningful.
  • Limit State Object Size: Avoid passing large objects (risk of data loss on page reload).
  • Resolvers over Route Data: Ensure data is loaded before the component is initialized.
  • Encode Sensitive Information: Do not expose sensitive information in URLs.
  • *Use trackBy with ngFor: Optimize performance when rendering lists from route data.

Angular provides various ways to transfer data between components through routes: from simple IDs in URLs to complex resolved data. By making the right choices in route parameters, query parameters, resolvers, and state objects, you can create flexible, user-friendly applications.

Pay attention to both security and performance, and choose the method that best fits your use case.

Creating custom directives in angular

 Creating Custom Data Directives

Step 1: Build an Attribute Directive

Example: Auto-Format Text on Input

  1. Create the directive:
ng generate directive autoFormat
  1. Define its behavior:
// auto-format.directive.ts
@Directive({
 selector: '[appAutoFormat]'
})
export class AutoFormatDirective {
 @HostListener('input', ['$event']) onInput(event: Event) {
  const input = event.target as HTMLInputElement;
  input.value = input.value.toUpperCase();
 }
}
  1. Use it in a template:
<input appAutoFormat placeholder="Type in uppercase">

Step 2: Build a Structural Directive

Example: Delay Element Rendering

  1. Create the directive:
ng generate directive delayRender
2.define logic:
// delay-render.directive.ts  
@Directive({  
  selector: '[appDelayRender]'  
})  
export class DelayRenderDirective {  
  constructor(  
    private templateRef: TemplateRef<any>,  
    private viewContainer: ViewContainerRef  
  ) {}  

  @Input() set appDelayRender(delay: number) {  
    setTimeout(() => {  
      this.viewContainer.createEmbeddedView(this.templateRef);  
    }, delay);  
  }  
}  
3.use it in template:
<div *appDelayRender="2000">This content renders after 2 seconds.</div>  

Directives in angular

Think of building a web application where every setting, list item, or condition for display requires manually updating the HTML. This would not only be a very time-consuming process, but it would also make for an ungainly codebase. Here Angular Data Directives step in at just the right moment—assigning meaningful content and movement to manipulated elements, regularly invoked whenever information changes throughout our UI. Still in doubt? Whether you're a programming beginner or an experienced professional, understanding directives is key to effective program design. This guide unravels Angular's directive world, provides useful examples, and presents expert knowledge to help you fully utilize its capabilities.

What Are Angular Directives?

Directives are DOM enhancements that extend HTML functionality. They create dynamic effects by binding data into elements. Angular divides directives into three categories:

  1. Components: Directives that include templates (e.g., @Component).
  2. Attribute Directives: Modify element appearance or behavior (e.g., ngClass, ngStyle).
  3. Structural Directives: Change the DOM structure by adding/removing elements (e.g., *ngIf, *ngFor,*ngSwitch).
directives in angular


Built-in Data Directives

1. Attribute Directives

NgClass

Use Case: Dynamically apply CSS classes based on data.

// component.ts
export class AppComponent {
 isActive = true;
}
<!-- component.html -->
<div [ngClass]="{ 'active': isActive, 'error': !isActive }">
 Status: {{ isActive ? 'Active' : 'Inactive' }}
</div>

NgStyle

Use Case: Conditionally apply inline styles.

// component.ts
export class AppComponent {
 progress = 60;
}
<!-- component.html -->
<div [ngStyle]="{
 'width': progress + '%',
 'background-color': progress >= 50 ? 'green' : 'red'
}">
 Progress: {{ progress }}%
</div>

NgModel (Two-Way Binding)

Use Case: Keep input fields in sync with component data.

// app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({ imports: [FormsModule] })
<!-- component.html -->
<input [(ngModel)]="username" placeholder="Enter username">
<p>Hello, {{ username }}!</p>

2. Structural Directives

*ngIf

Use Case: Conditionally render items.

<div *ngIf="user.isLoggedIn; else loginPrompt">
 Welcome, {{ user.name }}!
</div>

<ng-template #loginPrompt>
 <button (click)="login()">Log In</button>
</ng-template>

*ngFor

Use Case: Iterate over lists and dynamically generate items.

// component.ts
export class AppComponent {
 frameworks = ['Angular', 'React', 'Vue'];
}
<!-- component.html -->
<ul>
 <li *ngFor="let framework of frameworks; index as i">
  {{ i + 1 }}. {{ framework }}
 </li>
</ul>

*ngSwitch

Use Case: Manage multiple conditional cases.

<div [ngSwitch]="userRole">
 <p *ngSwitchCase="'admin'">Admin Dashboard</p>
 <p *ngSwitchCase="'editor'">Editor Tools</p>
 <p *ngSwitchDefault>Guest View</p>
</div>

Best Practices for Angular Directives

  • Use built-in directives like *ngIf and *ngFor whenever possible instead of reinventing the wheel.
  • Avoid direct DOM manipulation. Angular provides Renderer2 for cross-platform compatibility.
  • Optimize performance using trackBy in *ngFor to limit unnecessary re-renders.
  • Keep directive logic concise and reusable.
  • Validate directive behavior using Angular's testing utilities.

With Angular data directives, you can easily transform static pages into real-time interactive systems. By leveraging built-in directives like *ngIf and *ngFor and developing custom ones for specialized tasks, you can build an efficient and responsive application. When creating custom directives, ensure they are simple, focused on a single purpose, and avoid direct DOM manipulation for better maintainability across platforms. Optimize *ngFor for performance and always test your directives using Angular’s built-in utilities.

Are you ready to innovate your Angular project? Start using these directives now!

Select Menu