Angular 中的 ngIf 出错
Getting error with ngIf in Angular
我正在使用 angular,我想显示来自 json 形式的变量的数据
.我可以为 *ngfor 做到这一点,但我也想设置一个条件来检查是否 msg.who=="Bot"。我正在使用 :
<div class="UserWrapper" *ngFor="let msg of msgs" ngIf="msg.who == 'User ">
<div class=""></div>
<div class = "speech-bubble1 z-depth-5"><p>{{msg.msg}}</p>
<p class="timeRight">{{msg.time}}</p>
</div>
</div>
我不知道如何使用 ng if 另一种方式,但是在执行此操作时我收到以下错误。
ERROR Error: StaticInjectorError(AppModule)[NgForOf -> TemplateRef]:
StaticInjectorError(Platform: core)[NgForOf -> TemplateRef]:
NullInjectorError: No provider for TemplateRef!
如何检查给定条件
您不能在同一个 [=13 上使用 ngFor
和 ngIf
=],所以用ng-container
代替,也有一些错误,例如*ngIf
而不是ngIf
并且在检查类型时也应该使用 ===
按如下方式更改您的代码,
<ng-container *ngFor="let msg of msgs">
<div *ngIf="msg.who === User" class="">
<div class = "speech-bubble1 z-depth-5"><p>{{msg.msg}}</p>
<p class="timeRight">{{msg.time}}</p>
</div>
</ng-container>
我正在使用 angular,我想显示来自 json 形式的变量的数据 .我可以为 *ngfor 做到这一点,但我也想设置一个条件来检查是否 msg.who=="Bot"。我正在使用 :
<div class="UserWrapper" *ngFor="let msg of msgs" ngIf="msg.who == 'User ">
<div class=""></div>
<div class = "speech-bubble1 z-depth-5"><p>{{msg.msg}}</p>
<p class="timeRight">{{msg.time}}</p>
</div>
</div>
我不知道如何使用 ng if 另一种方式,但是在执行此操作时我收到以下错误。
ERROR Error: StaticInjectorError(AppModule)[NgForOf -> TemplateRef]:
StaticInjectorError(Platform: core)[NgForOf -> TemplateRef]:
NullInjectorError: No provider for TemplateRef!
如何检查给定条件
您不能在同一个 [=13 上使用 ngFor
和 ngIf
=],所以用ng-container
代替,也有一些错误,例如*ngIf
而不是ngIf
并且在检查类型时也应该使用 ===
按如下方式更改您的代码,
<ng-container *ngFor="let msg of msgs">
<div *ngIf="msg.who === User" class="">
<div class = "speech-bubble1 z-depth-5"><p>{{msg.msg}}</p>
<p class="timeRight">{{msg.time}}</p>
</div>
</ng-container>