7

试图寻找这个问题的答案,但没有成功。

在 angularJS 中使用 ng-class 时,是否有一个属性的值是类名?

我的意思的一个例子:

var things = [
        {
            a: "abc",
            aTrue: true
        }
];

然后在 Angular 中(在本例中使用 ng-repeat)

<div ng-repeat="thing in things" ng-class="{thing.a: !!thing.aTrue}></div>

我正在寻找类名"abc"- 但这给了我一个类名"thing.a". 这甚至可能吗,我哪里出错了?

提前致谢,感谢您的帮助。

4

1 回答 1

8

不起作用的原因是因为它的行为就像一个 Javascript 对象,所以你不能在 javascript 中做到这一点,你可以吗?

var test = 'hello';

var object = {
    test: 'hi'
};

object[test] === undefined // true // or
object.hello === undefined // true 
object.test  === undefined // false 

所以你不能key用这样的变量创建一个。所以尝试这样的事情。

 ng-class="{ true: thing.a, false: '' }[thing.aTrue]"

演示:http: //jsfiddle.net/XzXzR/1/

这是做什么的(javascript中的解释)

var test = {
   one: 'hello',
   two: 'world'
}['one'];

测试等于什么?

test ===  Object {one: "hello", two: "world"} // false
test ===  Object {one: "hello"} // false
test ===  Object {two: "world"} // false
test === 'one'   // false
test === 'two'   // false
test === 'hello' // ** true **
test === 'world' // false
于 2013-09-13T13:43:02.247 回答