• Join StackChief
  • Blog
  • Tutorials
  • Questions
  • React
  • JavaScript
  • MongoDB
  • NodeJs
  • Kafka
  • Java
  • Spring Boot
  • Examples

Blog

ngAfterViewChecked Example | Angular

Key Takeaways

  • ngAfterViewChecked is part of Angular's lifecycle hooks, ideally used after all child components are initialized and checked.
  • This hook is commonly used to handle scenarios where changes in the view need to be monitored post-checks.
  • Be cautious using it for DOM manipulation as it can be triggered frequently, impacting performance.

Understanding ngAfterViewChecked

ngAfterViewChecked() is an Angular lifecycle hook that gets called after the component's view and its child views have been checked. This occurs after ngAfterContentChecked(). It's essential for detecting changes that Angular doesn't automatically track within its change detection infrastructure.

Code Example

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

@Component({
  selector: 'app-home',
  template: `Click me`,
  styleUrls: ['./home.component.css']
})

export class HomeComponent implements AfterViewChecked {
  constructor() { }

  ngAfterViewChecked() {
    console.log("after view checked");
  }

  clickMe() {
    // Perform logic here potentially affecting the view
    console.log("link clicked");
  }
}

The updated example focuses on the core hook itself. Note how ngAfterViewChecked is standalone here—it doesn't require OnInit, as initialization tasks aren't relevant to the demonstration of this hook. This setup illustrates its primary utility: capturing post-render checks of the view.

When and Why to Use ngAfterViewChecked

Use ngAfterViewChecked when you need to perform operations after Angular view checks, such as running animations or updating the UI based on data changes that Angular’s change detection doesn’t automatically cover. It’s vital in scenarios where reactive programming and triggers beyond Angular’s automatic lifecycle management are necessary.

Cautions and Considerations

Be mindful of the potential performance pitfalls when using ngAfterViewChecked. Because it runs after every change detection cycle in the component's and child components' view, operations within this hook can lead to performance issues if not managed properly. Large computation tasks or frequent DOM access can cause slowdowns and should be handled carefully, often offloading intensive work elsewhere.

Use Cases

Common use cases for ngAfterViewChecked include recalculating styles based on dynamic content, handling errors not directly linked to specific components, or integrating with libraries and tools outside of Angular that need to be managed manually.

FAQ

How often does ngAfterViewChecked get called?

ngAfterViewChecked is called after every change detection cycle for the component and its child views. Frequent calls can lead to performance issues if not optimized.

Is it safe to manipulate the DOM in ngAfterViewChecked?

Direct DOM manipulation should generally be avoided in ngAfterViewChecked, as it can lead to performance problems. Instead, use Angular directives and bindings for better performance and code maintainability.

What's the difference between ngAfterViewChecked and ngAfterViewInit?

ngAfterViewInit is called only once after the first check of the component's views, while ngAfterViewChecked is called after each view check cycle, which can occur multiple times.

Can I perform HTTP requests in ngAfterViewChecked?

While possible, performing HTTP requests in ngAfterViewChecked isn't recommended due to potential performance hits and asynchronous timing issues. Consider placing such logic in a different lifecycle hook like OnInit or in reaction to user actions.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews
Comment