-4

I have referenced a control with ElementRef of Angular and for this I use this code:

HTML :

<button #btnSaveGeneralData...

Typescript :

@ViewChild ('btnSaveGeneralData') btnSaveGeneralData: ElementRef;

I need to reference to be able to reach the click () event, with javascript I have solved it:

let buttonDG: any = document.getElementsByName ('btnGaveDG') [0];
buttonDG.click ();

but doing so with ElementRef does not work, I get the error "... nativeElement undefined":

this.btnSaveGeneralData.nativeElement.click ();

I could do it with javascript but it's not the idea. Any suggestions, in which I can be wrong?

Thank you.

configbug
  • 746
  • 12
  • 19

1 Answers1

1

It works just fine.

Maybe your HTML isn't initialized yet, so the ElementRef would be undefined.
Or you have a spelling error.

Functioning Stackblitz

mahalde
  • 709
  • 5
  • 19