if else 在 Angular 中的 ngFor 循环内 8+

if else inside of the ngFor loop in Angular 8+

我想知道是否有将 if else 语句放入 for 循环的选项。

如果有机会将此 PHP 代码转换为 angular.

,我这里有示例

PHP 示例

while($row = mysqli_fetch_array($query)) {
   $section = $row['name'];

   if($section_id == "TEST_ID") {
      $section = "APPROVED";
   }
   else {
      $section = "DECLINED";
   }

   echo $section;

}

ANGULAR 代码

注意:此示例代码仅供参考。

<tr *ngFor="let student of students">
    <td>{{ student.student_id | uppercase }}</td>
    <td>{{ student.name }}</td>
    <td>{{ student.section }}</td>
    <td><button class="btn btn-sm btn-primary btn-block" (click)="edit(student)">EDIT</button></td>
    <td><button class="btn btn-sm btn-danger btn-block" (click)="delete(student)">DELETE</button</td>
</tr>

我研究过这个问题,但我很困惑,因为我是这个框架的初学者。请帮我解决这个问题,谢谢。

是的,你可以使用指令

<tr *ngFor="let student of students">
    <td>{{ student.student_id | uppercase }}</td>
    <td>{{ student.name }}</td>
    <td *ngIf="student.sectionId == TestID">APPROVED</td>
    <td *ngIf="student.sectionId != TestID">DECLINED</td>
    <td><button class="btn btn-sm btn-primary btn-block" (click)="edit(student)">EDIT</button></td>
    <td><button class="btn btn-sm btn-danger btn-block" (click)="delete(student)">DELETE</button</td>
</tr>