【发布时间】:2019-05-06 21:02:35
【问题描述】:
我正在使用 Angular 7。我得到了这些 CSS 声明:
.carddiv
{
margin: 0 auto;
width: 75%;
cursor: pointer;
}
.gameMessages
{
text-align: center !important;
font-size: 3em !important;
}
.messageSuccess
{
color : green;
}
.messageFailure
{
color: red;
}
这是 HTML:
<section class="carddiv">
<section class="gameMessages">
<p [ngClass] = "{'messageSuccess':booli === true,
'messageFailure':booli === false}">{{currentMoveMessage}} </p>
</section>
<mat-card class="cardclass" [attr.data-index]="i" *ngFor="let card of cards; index as i" (click)="onCardClick($event)">
<mat-card-header class="cardheader">
<mat-card-title> a card </mat-card-title>
</mat-card-header>
<img [src]="cards[i].pathToShow">
</mat-card>
</section>
在组件的类中:
export class CardcomponentComponent implements OnInit
{
booli : boolean;
currentMoveMessage : string;
constructor(public cardService: CardService)
{
this.booli = true;
this.currentMoveMessage = "message";
...
}
...
当我加载页面时,文本的颜色没有影响。当我直接使用样式时,它可以工作,这意味着这样做时:
<p style="color:red"> {{currentMoveMessage}} </p>
ngClass 有什么问题?
【问题讨论】:
-
这里肯定有更多内容,只是在堆栈闪电战中重新创建似乎很好。请编辑更多代码。
-
它与样式一起使用的部分直接让我感到困惑......
-
^^ 我猜测问题的可读性,Yon,是正确指向 css 的组件,您可以使用正常使用的类之一,即。 class="消息成功"。这可能是语法看起来正确的问题
-
也许可以试试 .gameMessages { text-align: center !important;字体大小:3em!重要; } 可能有其他样式干扰?
-
哈,那行得通..为什么我不能进行 booli === true 比较?