Angular – Waiting until ngIf is ready to avoid undefined error for elementRef inside it

The input element is undefined after inputVisibleIfThisIsTrue = true because it is inside an ngIf, I could use a delay but what if use transitions and effects I don’t want to worry about how much time the delay is set to.

if (this.something) {
    this.inputVisibleIfThisIsTrue = true;
    this.input.nativeElement.focus(); // this.input is undefined because ngIf takes time
}

So how can I only use this.input.nativeElement.focus() when the ngIf is ready? Is there an event that an observer could subscribe to?

Source: Angular

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.