【问题标题】:Can't bind to 'ngClass' since it isn't a known property of 'button' [duplicate]无法绑定到“ngClass”,因为它不是“按钮”的已知属性 [重复]
【发布时间】:2016-12-01 04:47:51
【问题描述】:

HTML:

<button class="sa-button" [ngClass]="'buttonClass'">{{displayText}}</button>

我希望在我的打字稿文件中绑定的变量:

public buttonClass = "button";

我得到的错误:

无法绑定到“ngClass”,因为它不是“按钮”的已知属性

我想绑定一个变量,该变量包含我要应用的 css 类的名称。

有没有正确的方法来做到这一点?我从 Angular 文档中直接复制了它,但它不起作用。

【问题讨论】:

  • 如果根模块下的组件确保已导入 BrowserModule。如果它在子模块中,则导入 CommonModule。在尝试使用通用指令时,缺少这些导入会给您带来错误。
  • JayChase 是正确的。 Angular 文档假定您已将所有组件插入到单个模块中。因此,如果您的应用程序是模块化的并且您没有正确的角度模块导入,那么ngClass 将无法按预期工作。
  • JayChase 的解决方案对我不起作用。导入 CommonModule,仍然收到 ngClass 错误。

标签: angular typescript


【解决方案1】:

通过添加单引号,您将在单引号中传递文本。

你想像这样删除它们......

<button class="sa-button" [ngClass]="buttonClass"  >{{displayText}}</button>

有关 ngClass 的更多信息,请查看docs

【讨论】:

猜你喜欢
  • 2019-03-11
  • 2018-11-25
  • 2021-12-02
  • 2021-03-23
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
相关资源
最近更新 更多