devidove
devidove

Reputation: 125

Wait for element to render after an *ngIf in Angular 5?

Is there a way to wait for a HTML element to render after it has been enabled because of an *ngIf ?

StackBlitz to show what I mean exactly.

I can't do document.getElementById('someid') if I just toggled the condition of the *ngIf on an element. Is there a callback/way to call the code just after the element has been rendered?

I just want it called on a specific element, so I can't just use ngAfterViewChecked().

Upvotes: 2

Views: 7917

Answers (2)

erni
erni

Reputation: 176

You can do html render first setting the function into a setTimeout

if(this.render) {
  setTimeout( () => document.getElementById('someid').focus());
}

Upvotes: 3

bc1105
bc1105

Reputation: 1270

Check this blitz: https://angular-hwpfwx-xvmbiv.stackblitz.io Open the console to see the reference to the div.

It uses ViewChild to detect the same div using ngIf.

Upvotes: -1

Related Questions