How To Update a Component Without Refreshing Full Page – Angular

Learn how to dynamically update Angular components without reloading the entire page using observables, services, and change detection...4 min


How-To-Update-a-Component-Without-Refreshing-Full-Page-Angular

🧑‍💻 How to Update a Component Without Refreshing the Full Page in Angular (Step-by-Step Guide)

Single Page Applications (SPAs) built with Angular are designed to be dynamic and responsive. But sometimes, updating a specific component without triggering a full-page refresh can be tricky, especially when data changes in a sibling or child component.

In this article, we’ll explore how to update Angular components dynamically without reloading the full page using services, observables, and change detection strategies. We’ll also walk through practical examples to help you implement this in your project easily.


⚙️ Common Use Case

Let’s say you have a dashboard page with a NotificationComponent and a TaskComponent. When a new task is created in TaskComponent, You want the NotificationComponent to update automatically without a full page reload.


✅ Solution Overview

To update a component without reloading the entire page, use:

✅ Shared Angular Service (for communication)

✅ BehaviorSubject or Subject (for reactive data flow)

✅ ngOnInit() lifecycle method (to subscribe and update)

✅ ChangeDetectorRef if manual detection is required


🧩 Step-by-Step Example

Let’s walk through a real Angular example.

🔧 Step 1: Create a Shared Service

Create a service to hold the shared observable data.

ng generate service shared/data
// data.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
  providedIn: 'root',
})
export class DataService {
  private taskUpdateSource = new BehaviorSubject<boolean>(false);
  taskUpdated$ = this.taskUpdateSource.asObservable();
  notifyTaskUpdate() {
    this.taskUpdateSource.next(true);
  }
}

📤 Step 2: Emit Event from Task Component

// task.component.ts
import { Component } from '@angular/core';
import { DataService } from '../shared/data.service';
@Component({
  selector: 'app-task',
  template: `
    <button (click)="createTask()">Add Task</button>
  `,
})
export class TaskComponent {
  constructor(private dataService: DataService) {}
  createTask() {
    // Your task creation logic
    console.log('Task created');
    // Notify NotificationComponent
    this.dataService.notifyTaskUpdate();
  }
}

📥 Step 3: Listen in Notification Component

// notification.component.ts
import { Component, OnInit } from '@angular/core';
import { DataService } from '../shared/data.service';
@Component({
  selector: 'app-notification',
  template: `
    <div *ngIf="showUpdate">📢 A new task has been added!</div>
  `,
})
export class NotificationComponent implements OnInit {
  showUpdate = false;
  constructor(private dataService: DataService) {}
  ngOnInit() {
    this.dataService.taskUpdated$.subscribe((status) => {
      this.showUpdate = status;
      // Optionally reset after 3 seconds
      setTimeout(() => {
        this.showUpdate = false;
      }, 3000);
    });
  }
}

💡 Bonus: Use ChangeDetectorRef for Manual Triggers


In rare cases, Angular might not detect the change (especially in OnPush components). In such cases, you can use ChangeDetectorRef:

import { ChangeDetectorRef } from '@angular/core';
constructor(private cd: ChangeDetectorRef) {}
someMethod() {
  this.cd.detectChanges();
}

🔄 Alternate Method: Using @Input() and ngOnChanges

If you’re passing data from a parent to a child component and want to refresh it without reloading:

// parent.component.html
<app-child [task]="currentTask"></app-child>
// child.component.ts
@Input() task: any;
ngOnChanges(changes: SimpleChanges) {
  if (changes['task']) {
    this.refreshView();
  }
}

📱 Mobile Friendly Tip

All dynamic updates in Angular are handled in the DOM without reloading the entire HTML document. This makes Angular ideal for mobile-first applications, ensuring smooth and fast user experiences.


🚀 Conclusion

With Angular’s reactive architecture, updating a component without reloading the page is not just possible — it’s recommended for building high-performance applications. Whether you’re working with services, @Input(), or change detection strategies, Angular gives you full control over how components behave dynamically.


📚 Related Resources


Discover more from 9Mood

Subscribe to get the latest posts sent to your email.


Like it? Share with your friends!

What's Your Reaction?

Lol Lol
0
Lol
WTF WTF
0
WTF
Cute Cute
0
Cute
Love Love
0
Love
Vomit Vomit
0
Vomit
Cry Cry
0
Cry
Wow Wow
0
Wow
Fail Fail
0
Fail
Angry Angry
0
Angry
Being Coders

Newbie

A Tech publication for all. Get Knowledge about Web Development, Programming, and trending news, tutorials, and tools for beginners to experts.

0 Comments

Leave a Reply

Choose A Format
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Ranked List
Upvote or downvote to decide the best list item
Open List
Submit your own item and vote up for the best submission
Countdown
The Classic Internet Countdowns
Meme
Upload your own images to make custom memes
Poll
Voting to make decisions or determine opinions
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Personality quiz
Series of questions that intends to reveal something about the personality
is avocado good for breakfast? Sustainability Tips for Living Green Daily Photos Taken At Right Moment