MariusJ MariusJ - 10 months ago 46
TypeScript Question

Style HTML object using component function

<table *ngIf="datoer">
<td *ngFor="let cell of ukeEn()">
<div class="outer" *ngIf="datoerContains(cell) != null">
<div class="circle" id="circle" *ngFor="let circle of datoerContains(cell)">
// <script>
// document.getElementById("circle").style.background-color = anyFunction();
// </script>
<div class="details" *ngFor="let circle of datoerContains(cell)">
{{circle.skole}} <br>
{{circle.kommentar}} <br>
SFO: {{circle.sfodag}}


This is written in html with Angular 2.
What I want to do, is to set the
background color
of class
by calling a function from my
file which returns a string like
. Check the comments on how I want it to work.
Is this possible? If yes, how?

Answer Source

You can use style binding or the ngStyle directive:

<div class="circle" id="circle" [style.background-color]="anyFunction()"
<div class="circle" id="circle" [ngStyle]="{'background-color': anyFunction()"

Directly binding to functions is discouraged though, because these functions are called every change detection cycle. It's usually more efficient to assign the result of such a function to a property and bind from the view to that property.