让 class 工作

Getting ng-class to work

当我在 w3schools.com Try It page 中测试 ng-class 时,一切正常,如文档所述。当我在一个简单的 Angular 2 应用程序中尝试它时,我一点也不高兴。

这是我的步骤。在终端 window:

  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 中,文本会变红。

我错过了什么?

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

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

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