【问题标题】:Getting ng-class to work让 ng-class 工作
【发布时间】:2017-05-08 14:17:47
【问题描述】:

当我在 w3schools.com Try It page 中测试 ng-class 时,一切正常,如文档所述。当我在一个简单的 Angular 2 应用程序中尝试它时,我没有得到任何乐趣。

这是我的步骤。在终端窗口中:

  1. ng new classTest
  2. cd classTest
  3. ng serve

在文本编辑器中:
app.component.css

.red {
  color: #f00;
}

app.component.html

<h1 ng-class="{'red': true}">
  {{title}}
</h1>

结果:文本 app works! 显示为黑色,而不是红色。我已经尝试了许多其他变体,例如 ng-class="'red'",在 w3schools.com 中 Try It page,文字随你喜欢变红。

我错过了什么?

【问题讨论】:

    标签: angular angular-directive angular-template


    【解决方案1】:

    这是 AngularJS 示例,Angular 2 中 NgClass 指令的语法如下:

    <h1 [ngClass]="{'red': true}">
        {{title}}
    </h1>
    

    您可以阅读有关NgClass 指令here 的更多信息。

    【讨论】:

      猜你喜欢
      • 2017-03-18
      • 1970-01-01
      • 2014-03-08
      • 2016-08-26
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多