An error is being thrown from the below code, but the code is still working as expected.
import { ..., ElementRef } from '@angular/core';
constructor(
...,
private elementRef: ElementRef
) { }
ngOnInit(): void {
const keyword: string = 'xyz';
this.elementRef.nativeElement.setAttribute('key', keyword); // gives error
}
Error -
Unsafe member access .setAttribute on an 'any' value. eslint(@typescript-eslint/no-unsafe-member-access)
Whats's the fix?
I was trying to set 'keyword as string', but not working.
this.elementRef.nativeElement.setAttribute('key', keyword as string);
Solution -
const element: HTMLElement = this.elementRef.nativeElement as HTMLElement;
element.setAttribute('key', keyword);