Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize ElementRef Angular - Typescript

when I use

@ViewChild('serachField', { static: false }) serachField: ElementRef<HTMLInputElement>

I get the error

Property 'serachField' has no initializer and is not definitely assigned in the constructor.

I usually initialise the variable, But I'm unsure how to do it in this case

I've tried

@ViewChild('serachField', { static: false }) serachField: ElementRef<HTMLInputElement> = new ElementRef<HTMLInputElement>(--unsure what the parameter should be--)

I get:

Expected 1 arguments, but got 0.ts(2554)
core.d.ts(2253, 17): An argument for 'nativeElement' was not provided.
like image 859
Rasta Avatar asked Nov 26 '25 03:11

Rasta


1 Answers

Thank you @Isbn

I used:

@ViewChild('serachField', { static: false }) serachField: ElementRef<HTMLInputElement> = {} as ElementRef;
like image 140
Rasta Avatar answered Nov 28 '25 15:11

Rasta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!