【问题标题】:AngularJs:-Add multiple conditions in ternary operations of ng-classAngularJs:-在ng-class的三元运算中添加多个条件
【发布时间】:2016-12-10 13:24:31
【问题描述】:

我想在ng-class属性中条件表达式为真的时候整合多个类..

以下是我正在尝试但无法正常工作的代码:

<div class="col-md-4" ng-mouseover="hoverButton=true"  id="plain_text" ng-class="$state.current.name==='home' && 'hoverButton' ? 'tint,btn-white,panel_hover_font' : 'sd'" style="border: 1px solid;min-height: 300px;">

当条件为真时,我无法整合tint,btn-white,panel_hover_font。如何实现?

【问题讨论】:

  • 删除'hoverButton'周围的引号。
  • @Tushar 当条件为真时,我无法集成三个 css 类

标签: javascript css angularjs ternary-operator ng-class


【解决方案1】:

多个 CSS 类用空格分隔,而不是逗号:

ng-class="$state.current.name === 'home' && hoverButton ? 'tint btn-white panel_hover_font' : 'sd'"

另外,我假设hoverButton 是一个变量,而不是字符串'hoverButton'

【讨论】:

    【解决方案2】:

    我不知道你是否可以使用三元运算符,但你可以使用简单的条件运算符:

    <span ng-class="{'a set of classes': condition.isTrue, 'another set of classes': condition.isFalse }"> </span>
    

    我在一个真实的项目中使用它并且它正在工作。在您的情况下,它将给出以下内容

    <div class="col-md-4" ng-mouseover="hoverButton=true" id="plain_text" ng-class="{ 'tint btn-white panel_hover_font': ($state.current.name==='home' && 'hoverButton'),  'sd': !($state.current.name==='home' && 'hoverButton') } " style="border: 1px solid; min-height: 300px;">
    

    【讨论】:

    • Done o/ 但是我认为接下来的答案用三元运算符解决了这个问题,只需检查一下。
    【解决方案3】:

    this fiddle

    您必须将ng-class="'hoverButton' ? 'red, anotherclass, another' : 'white'" 更改为ng-class="hoverButton ? 'red anotherclass another' : 'white'"

    我编写了一些 css 类用于测试。

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 2014-05-08
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多