正确的输出未显示在Angular定义NGIF时



我目前是Angular的新手显示。

import { Component} from '@angular/core';
@Component({
    selector: 'newlearn',
    template:`
        <div *ngIf="courses.length > 0">
            List of Courses
        </div>
        <div *ngIf="courses.length == 0">
            Not yet
        </div>
})
export class NewlearnComponent 
{
    courses=[1,2];
}

预期结果:课程列表

实际结果:课程清单课程清单尚未

我认为您不想使用标签,因此您可以按照以下方式进行三元运算符

{{courses.length > 0?' List of Courses':' Not yet'}}

或您使用ng-template

<ng-template [ngIf]="courses.length > 0" [ngIfElse]="notyet">
    List of Courses
</ng-template>
<ng-template #notyet>
   Not yet
</ng-template>

希望这会有所帮助。

最新更新