【问题标题】:Can i write a CSS class under conditional [ngCLASS]我可以在条件 [ngCLASS] 下编写 CSS 类吗
【发布时间】:2017-12-13 20:34:12
【问题描述】:

我在编写逻辑时确实有一些问题。 我正在使用 Angular2。 在 html 页面中,我有两个按钮 - 是和否 我想要 YES 和 NO 不同的颜色。 所以在 div 标签中,我有这样的条件:

ngClass="'result'?'yes':'no'"

现在我如何使用 CSS 类。我有类 green-button 代表 YES 和 red-button 代表 NO。
如何在 [ngClass] 中使用它们。

【问题讨论】:

  • 它包含来自 API 的 yes 和 no 值。
  • 我在按钮中显示文本。我的查询是更改颜色 w.r.t 是和否。
  • 如果答案是肯定的,那么绿色按钮或红色按钮。
  • 如果result = 'yes' ==> 添加类是。如果result = 'no' ==> 添加类号。对吗?
  • 是的@Duannx Ri8

标签: jquery css angular angular2-template angular2-forms


【解决方案1】:

你可以试试这个:

<div class=" form-group" [ngClass]="{ (result==='YES')? 'green-button': 'red-button'}">

希望对你有帮助..:)

【讨论】:

    【解决方案2】:

    如果你想根据不同的条件应用不同的类,你可以这样做

    <div class=" form-group" [ngClass]="{'green-button': green-button-condition, 'red-button': red-button-condition}">
    

    green-button-conditionred-button-condition 替换为您的条件

    【讨论】:

    • 这是正确的@umar 但我有条件在''? ' : ' ' 像这样。
    • [ngClass]="'result'?'yes':'no'" 这行应该存在。现在更新这个。任何想法。
    • 您需要将您的条件重新表述为角度方式
    • [ngClass]="{'green-button': result=='yes', 'red-button': result=='no'}"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 2019-08-15
    • 2012-10-20
    • 1970-01-01
    • 2014-04-15
    • 2020-06-15
    • 2010-09-05
    相关资源
    最近更新 更多