Last updated Jan 14, 2025

How to use @ViewChild with *ngIf in Angular18+

Use @ViewChild with *ngIf correctly in Angular 18+.

How to use @ViewChild with *ngIf in Angular18+

When working with Angular, you may need to use the *ngIf directive to conditionally render components. However, if you try to access a view child using @ViewChild that is hidden by *ngIf, you may run into issues. In this article, we'll look at how to use @ViewChild with *ngIf in Angular.

The problem

When you use *ngIf to conditionally render a component, Angular will create or destroy the component based on the condition. If you have a view child that depends on this component, you may run into issues trying to access the view child.

Here's an example of how you might define a view child for a component with *ngIf:

@Component({
  selector: 'app-my-component',
  template: `
    
` }) export class MyComponent implements AfterViewInit { @ViewChild('myTabs', { static: false }) myTabs!: TabsetComponent; ngAfterViewInit() { console.log(this.myTabs); } }

In this code, we have a TabsetComponent inside an *ngIf directive. We use @ViewChild to define a view child for the TabsetComponent. In the ngAfterViewInit hook, we try to access the view child.

However, if showTabs is false, the TabsetComponent won't exist in the DOM, and we'll get a undefined value for myTabs.

Read, How To Implement Role-Based Access Control With JWT-Based Authorization In Angular

The solution

To handle the *ngIf condition and ensure that the view child is properly initialized, we can use a setter method with the @ViewChild decorator. Here's an updated version of our MyComponent:

@Component({
  selector: 'app-my-component',
  template: `
    
` }) export class MyComponent implements AfterViewInit { private _myTabs?: TabsetComponent; @ViewChild('myTabs', { static: false }) set myTabs(value: TabsetComponent) { if (value) { this._myTabs = value; console.log(this._myTabs); } } ngAfterViewInit() { console.log(this._myTabs); } }

In this code, we define a private property _myTabs to store the TabsetComponent instance. We use a setter method with the @ViewChild decorator to set this property when the TabsetComponent is initialized.

Inside the setter method, we check whether value is truthy before assigning it to _myTabs. This ensures that we don't try to access the view child before it's fully initialized.

In the ngAfterViewInit hook, we can access the _myTabs property and use it as needed.

Also read, Optimize Page Load Speed With Lazy Loading Images In Angular 15

Conclusion

Using @ViewChild with *ngIf in Angular can be a bit tricky, but by using a setter method, we can handle the condition and ensure that the view child is properly initialized.

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

0 comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

No comments yet — be the first to start the discussion.

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.