2
<table *ngIf="datoer">
        <tr>
          <th>Man</th>
          <th>Tir</th>
          <th>Ons</th>
          <th>Tor</th>
          <th>Fre</th>
          <th>Lør</th>
          <th>Søn</th>
        </tr>
        <tr>
            <td *ngFor="let cell of ukeEn()">
                {{cell.text}}
                <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>
              <div class="details" *ngFor="let circle of datoerContains(cell)"> 
                    {{circle.skole}} <br>
                    {{circle.kommentar}} <br>
                    SFO: {{circle.sfodag}}
                  </div>

                </div>
            </td>            
        </tr>
</table>

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, thanks! What do you mean with 'bind from the view to that property'? Like for example anyFunction().color?
For example you have an @Input set color(value: String) { this.backgroundColor = anyFunction(value); } and then use [style.background-color]="backgroundColor". This way you don't bind to a function directly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.