本文介绍了应用特定的类或使用ng-class的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据某个变量的值来应用某个类.我知道通常如何使用ng-class,但是我的情况有点特殊.

I would like to apply a certain class depending the value of a variable.I know how to use ng-class usually, but my case is a bit special.

这是我的代码:

<div ng-class="{'col-lg-12 col-md-12 col-ms-12 col-xs-12': field.LongQuestion, subCategory.DefaultSize: !field.LongQuestion}">

如果"LongQuestion"为真,我想将类设置为" col-lg-12 col-md-12 col-ms-12 col-xs-12 ",否则,我想要使用存储在'DefaultSize'中的值(在这种情况下为' col-lg-5 col-md-5 col-ms-12 col-xs-12 ').

If "LongQuestion" is true, I want to set the class to 'col-lg-12 col-md-12 col-ms-12 col-xs-12', otherwise, I want to use the value stored in 'DefaultSize' (which is, in this case, 'col-lg-5 col-md-5 col-ms-12 col-xs-12').

我收到此错误:

有什么想法如何在ng-class变量中使用类值存储?

Any idea how to use a class value store in a variable with ng-class ?

推荐答案

<div ng-class="{'col-lg-12 col-md-12 col-ms-12 col-xs-12': field.LongQuestion, subCategory.DefaultSize: !field.LongQuestion}">

相反,您不能只为此创建一个函数吗?

Instead of this, couldn't you just create a function for it?

我会这样做:

<div ng-class="getCSSClass(field.LongQuestion, field.TrueClass, field.DefaultClass)"></div>

然后在函数中进行检查:

Then in the function you do the check:

  $scope.getCSSClass = function(class, trueClass, defaultClass) {
     if(class)
         return trueClass;
     else
         return defaultClass;
  }     

这篇关于应用特定的类或使用ng-class的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:27