MariusJ MariusJ - 1 month ago 11
HTML Question

Angular 2: if attr.title in html == array in component

<div class="kalender">
<table *ngIf="datoer">
<tr>
<td *ngFor="let cell of ukeEn()"
[attr.title]="cell.id">{{cell.text}}</td>
</div>
</tr>
</table>
</div>


I would like to check if
attr.title
(or
cell.id
) ==
any of the element's .dato attribute
(an element is a type element called
Cell
, with the attribute
dato
) in an array called
datoer
, which is defined in my component. So I need to check the
attr.title
up against every value in the
datoer
array. Then I want to add an element (for ex.
div
or
p
) in the
td
if this condition is
true
.

Is this possible?
If you need more code samle to understand my problem, just ask.

Answer
<div class="kalender">
    <table *ngIf="datoer">
        <tr>
            <td *ngFor="let cell of ukeEn()" [attr.title]="cell.id">
               <div *ngIf="datoerContains(cell)">{{cell.text}}</div>
            </td>            
        </tr>
    </table>
</div>

In your component put this:

datoerContains(cell:any):boolean {
   for(let i = 0; i < this.datoer.length; i++) {
      if (this.datoer[i].dato == cell.dato) {
         return true;
      }
   }
   return false;
}
Comments