Scipion
Scipion

Reputation: 11888

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 ?

Upvotes: 4

Views: 5252

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657308

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.

Upvotes: 8

Related Questions