ngSwitchCase 没有提供者

Tim*_*çek 2 html javascript css angular

我正在看教程并试图做我学到的东西。我在下面遇到错误。我不明白为什么我在浏览器控制台上有这个错误消息。它说[ERROR ->]<span *ngSwitchCase="true"> 但我不知道为什么它说 ngSwitchCase 是错误的。检查了所有文件和代码,但似乎没有问题。我的错误在哪里?

错误

Uncaught Error: Template parse errors:
No provider for NgSwitch ("       <td>{{item.description}}</td>
              <td [ngSwitch]="item.action"></td>
              [ERROR ->]<span *ngSwitchCase="true">
                  Yes
              </span>
"): ng:///AppModule/AppComponent.html@25:14
No provider for NgSwitch ("
                  Yes
              </span>
              [ERROR ->]<span *ngSwitchCase="false">
                  No
                </span>
"):
Run Code Online (Sandbox Code Playgroud)

应用程序组件.html

  <table class="table table-striped table-bordered">
                <thead>
                   <tr>
                     <th></th>
                     <th>Description</th>
                     <th>Action</th>
                   </tr>
                </thead>
                <tbody>
                <tr *ngFor="let item of items;let i=index">
                  <td>{{i+1}}</td>
                  <td>{{item.description}}</td>
                  <td [ngSwitch]="item.action"></td>
                  <span *ngSwitchCase="true">
                      Yes
                  </span>
                  <span *ngSwitchCase="false">
                      No
                    </span>
                </tr>
                </tbody>
              </table>
Run Code Online (Sandbox Code Playgroud)

app.comonent.ts

export class AppComponent {

  items:Model[]= [
    new Model('Breakfast',false),
    new Model('Sport',false),
    new Model('Studying',true),
    new Model('Cemo',false),
  ]
}
Run Code Online (Sandbox Code Playgroud)

模型.ts

export class Model
{
   description:string;
   action:Boolean;
   constructor(description:string,action:Boolean)
   {
       this.description=description;
       this.action=action;
   }
}
Run Code Online (Sandbox Code Playgroud)

Che*_*pan 7

您应该像这样将 span 包裹在 td 标签内,因为 ngSwitch 指令的范围是 td 元素而不是 td 元素之外

<td [ngSwitch]="item.action">
    <span *ngSwitchCase="true">
              Yes
    </span>
    <span *ngSwitchCase="false">
            No
    </span>
  </td>
Run Code Online (Sandbox Code Playgroud)