Angularjs 在条件下使用变量中的类生成ng类

Angularjs 在条件下使用变量中的类生成ng类,angularjs,ng-class,Angularjs,Ng Class,我使用的是一个指令,希望使用ng class,以便在特定条件下输出多个类(存储在变量中) <div ng-if="showIcons" ng-class="{state.icon: showIcons}"></div> 不幸的是,这不起作用。在ng类中使用三元运算符,您可以尝试以下操作吗 <div ng-if="showIcons" ng-class="showIcons ? state.icon : null"></div> 在ng类中使

我使用的是一个指令,希望使用
ng class
,以便在特定条件下输出多个类(存储在变量中)

<div ng-if="showIcons" ng-class="{state.icon: showIcons}"></div>

不幸的是,这不起作用。

ng类中使用三元运算符
,您可以尝试以下操作吗

<div ng-if="showIcons" ng-class="showIcons ? state.icon : null"></div>


ng类中使用三元运算符
,可以尝试以下操作吗

<div ng-if="showIcons" ng-class="showIcons ? state.icon : null"></div>


您可以通过将函数绑定到
ng类
来完成更复杂的工作参见我的

JS:

HTML:


{{c.title}}

我更喜欢这种方法,因为您可以在函数中变得复杂,甚至可以访问其他服务。

您可以通过将函数绑定到
ng类来完成更复杂的工作

JS:

HTML:


{{c.title}}

我更喜欢这种方法,因为您可能会在功能上变得复杂,甚至可以访问其他服务。

您使用的是哪个版本的Angular?Angular 1.5.5哪个版本的Angular?Angular 1.5。5@be-在这本书上,谢谢你的努力。也许我的问题出在别的地方。@我想是的。尝试硬编码你的类。它有效吗?
state.icon
是字符串吗?你确定你的类存在于CSS中吗?玩我的Plunker,也许你会从中解决你的问题。:-)如果我尝试硬编码,它会起作用,因此它必须是具有
状态值的东西。图标
:)就可以了。@在这个插件上进行编码。谢谢您的努力。也许我的问题出在别的地方。@我想是的。尝试硬编码你的类。它有效吗?
state.icon
是字符串吗?你确定你的类存在于CSS中吗?玩我的Plunker,也许你会从中解决你的问题。:-)如果我尝试硬编码,它会起作用,所以它必须是值为
state的东西。图标将起作用。
var app = angular.module('hey', []);

app.controller('TheController', [function () {
  this.title = "hey"
  this.shouldApply = true;

  this.canApplyClass = function () {
    if (this.shouldApply == true) {
      return 'happy sad other';
    }

    return '';
  }
}]);
<body ng-app="hey">
  <div ng-controller="TheController as c">
    <h1 ng-class="c.canApplyClass()">{{c.title}}</h1>  
  </div>
</body>