Home > Software engineering >  Access a nativeElement right after being pushed into ngFor loop?
Access a nativeElement right after being pushed into ngFor loop?

Time:06-20

I'm pushing a new item on a ngFor loop. Right after I'm trying to get its html reference (ElementRef nativeElement). But it's not "ready" yet. So I used a settimeout. Worked.

Is there any "elegant", angular way, solution instead of using settimeout?

I didn't post any code, because is a rather simple array.push and nativeElement use.

CodePudding user response:

You can use ngAfterViewInit() to get elements after the dom is loaded: https://angular.io/api/core/AfterViewInit

ngAfterViewInit():void {
  this.elem = document.querySelector('#elem');
}

CodePudding user response:

I used the ChangeDetectorRef method: detectChanges()

right after the .push() And worked perfectly.

  • Related