Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 getElementsByClassName on ElementRef

I have the following constructor in my component :

constructor (private el: ElementRef) {this.el = el.nativeElement}

then in my ngOnInit :

ngOnInit() {
 let foos = this.el.getElementsByClassName('foo')
}

triggers : TS2339: Property 'getElementsByClassName' does not exist on type 'ElementRef'.

I did try converting my ElementRef to an HTMLElement by without any success. Any idea how to deal with that error ?

like image 802
Scipion Avatar asked Feb 04 '23 19:02

Scipion


1 Answers

Remove private:

el:HtmlElement;
constructor (el: ElementRef) {this.el = el.nativeElement}

With your original code this.el will be declared as type ElementRef but then an HTMLElement will be assigned. This is why you get the error message.

like image 154
Günter Zöchbauer Avatar answered Feb 07 '23 09:02

Günter Zöchbauer