Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

element.style not working in typescript [Angular 5]

I am trying to set style of element with javascript, inside my typescript but it doesn't work. This is what I try to do:

const element = document.getElementsByClassName('current');
element.style.backgroundColor = "" + this.styles.style.mainIdentifyingColor;

but I get error:

Property 'style' does not exist on HTMLCollectionOf.

I also tried with setAttribute but same thing..

like image 473
Nemanja Grabovac Avatar asked Apr 03 '18 16:04

Nemanja Grabovac


1 Answers

I know am not exactly answering your question but did you try working with NgStyle ? docs


HTML

<div [ngStyle]="{ 'background-color': myCustomColor }"> 

</div>

TS

someFunctionToBeCalled() {
  this.myCustomColor=this.styles.style.mainIdentifyingColor;
}
like image 138
NaceurBouhamed Avatar answered Sep 28 '22 18:09

NaceurBouhamed