Нет реакции NgClass на функцию - PullRequest
0 голосов
/ 06 марта 2020

Я использую функцию в NgClass, которая использует массив, заполненный в ngOnInit.

ngOnInit -> prepareRezerwation () create colorRezerwation проверяемый:

    this.nodeService.getRezerwations(this.minMax).subscribe(rezerwations => { 
    this.prepareRezerwation(rezerwations);
    //   this.functionsService.showRezerwation(post, this.openingHours);
      // this.showSpinner = false;
    }, error => {
       console.log("Connection problem")
    }); 

html -> [ngClass] = "setColor (1, i):

<ng-container matColumnDef="big">
    <th mat-header-cell *matHeaderCellDef class="xunk-calendar-cell"></th>
    <td mat-cell *matCellDef="let element; let i = index" [ngClass]="setColor(1,i)">Name</td>
</ng-container>

setColor (1, i):

  setColor(colIndex, rowIndex){
    this.colorRezerwation.position.forEach(arrayItem => {
      if(arrayItem.column === colIndex && arrayItem.row === rowIndex){
        return {'reservation': true}
      } 
    });
  }

Когда я пропускаю весь возврат forEach, он работает нормально.

Спасибо за помощь.

1 Ответ

0 голосов
/ 06 марта 2020

Вы возвращаете переменную класса внутри функции в forEach l oop, а не в функцию setColor.

setColor(colIndex, rowIndex){
    this.colorRezerwation.position.forEach(arrayItem => {
      if(arrayItem.column === colIndex && arrayItem.row === rowIndex){
   -->  return {'reservation': true} <-- this does not return to setColor
      } 
    });
}

Вам необходимо объявить и хранить переменную вне forEach l oop и вернуть это:

setColor(colIndex, rowIndex){
    let class;
    this.colorRezerwation.position.forEach(arrayItem => {
      if(arrayItem.column === colIndex && arrayItem.row === rowIndex){
        class = {'reservation': true}
      } 
    });
    return class;
  }
...