【问题标题】:How to bind ngSwitch case in div tag in angular?如何在 div 标签中以角度绑定 ngSwitch 案例?
【发布时间】:2021-02-22 03:33:22
【问题描述】:

这是我要运行的代码:

    <div [ngStyle]="getStartTimeStyle()" [ngSwitch]="event?.time">
       Time: {{event?.time}}
    <span *ngSwitchCase="'8:00 am'">(Early start)</span>
    <span *ngSwitchCase="'10:00 am'">(Late Start)</span>
    <span *ngSwitchDefault>(Normal Start)</span>
  </div>

但它显示的错误如下:

错误 NG8002:无法绑定到“ngStyle”,因为它不是“div”的已知属性。

      <div [ngStyle]="getStartTimeStyle()" [ngSwitch]="event?.time">

无法绑定到“ngSwitch”,因为它不是“div”的已知属性。

    <div [ngStyle]="getStartTimeStyle()" [ngSwitch]="event?.time">

请帮忙,我该如何解决这个问题?

                                             ~~~~~~~~~~~~~~~~~~~~~~~~

【问题讨论】:

  • 遵循一个具有正确模块导入的教程示例,例如通用模块

标签: angular angular-ngmodel ng-style


【解决方案1】:

您需要在要使用“ngSwitch”和“ngStyles”指令的模块中导入“CommonModule”,因为这两个指令都是“CommonModule”的一部分。

import { CommonModule } from '@angular/common';

【讨论】:

    【解决方案2】:

    不要使用 div 而是使用 container_element

    ... ...

    ...

    我给你留下了文档的链接,以便你可以更详细地查看

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 2016-10-27
      • 2019-02-06
      • 2022-12-03
      相关资源
      最近更新 更多