Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象属性值作为类名,ng类_Javascript_Angularjs_Ng Class - Fatal编程技术网

Javascript 对象属性值作为类名,ng类

Javascript 对象属性值作为类名,ng类,javascript,angularjs,ng-class,Javascript,Angularjs,Ng Class,试图寻找这个问题的答案,但没有成功 在angularJS中使用ng类时,是否仍然需要将属性的值作为类名 我的意思的一个例子: var things = [ { a: "abc", aTrue: true } ]; 然后是角度(在本例中使用ng repeat) 之所以不起作用,是因为它的行为就像一个Javascript对象,所以你不能在Javascript中这样做,不是吗 var test = 'hello'; v

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

在angularJS中使用ng类时,是否仍然需要将属性的值作为类名

我的意思的一个例子:

var things = [
        {
            a: "abc",
            aTrue: true
        }
];
然后是角度(在本例中使用ng repeat)


之所以不起作用,是因为它的行为就像一个Javascript对象,所以你不能在Javascript中这样做,不是吗

var test = 'hello';

var object = {
    test: 'hi'
};

object[test] === undefined // true // or
object.hello === undefined // true 
object.test  === undefined // false 
因此,您不能使用这样的变量创建
键。所以试试这样

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

它的作用是(用javascript解释)

测试等于什么

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
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