在Angular中使用ngIf获取错误

ali*_*lia 7 angular-ng-if angular

我使用的角度,我想从一个变量,它是JSON形式.I'm能够做到这一点的显示数据*ngfor而且我希望把它检查是否msg.who =="博特"的条件我正在这样做:

<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>
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用ng如果另一种方式,但这样做我得到以下错误.

ERROR Error: StaticInjectorError(AppModule)[NgForOf -> TemplateRef]: 
StaticInjectorError(Platform: core)[NgForOf -> TemplateRef]: 
NullInjectorError: No provider for TemplateRef!
Run Code Online (Sandbox Code Playgroud)

我该如何检查给定的条件

Saj*_*ran 10

你不能使用ngFor和使用ngIf相同的element,所以更换它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>
Run Code Online (Sandbox Code Playgroud)