【发布时间】:2016-05-19 10:12:23
【问题描述】:
您好,我有一个包含列表的对象,我想在我的 html 中循环使用 ng-repeat。该列表是键值,值是真或假。在我的 html 中,我有一个带有类和自己的背景颜色的 div。因此,当我执行 ng-repeat 时,我想检查列表中的值是否为 false 并添加一个具有新背景颜色的新类。我在这个 ng-class 中使用了一个表达式,但它不起作用。这是我的对象:
$scope.list = [
{
"Name" : "Jacob",
"Properties" : {
"P1": true,
"P2": true,
"P3": false
}
},
{
"Name" : "James",
"Properties" : {
"P1" : false,
"P2" : true,
"P3" : true
}
},
{
"Name" : "Hector",
"Properties" : {
"P1" : false,
"P2" : false,
"P3" : true
}
}
]
然后我得到了follow html:
<div ng-repeat="item in list">
<div class="myContainer">
<div class="cMyBox cColor1" ng-class="{cWhite: !item.Properties.P1}"></div>
<div class="cMyBox cColor2" ng-class="{cWhite: !item.Properties.P2}"></div>
<div class="cMyBox cColor3" ng-class="{cWhite: !item.Properties.P3}"></div>
</div>
</div>
这是我的 CSS:
.myContainer {display: flex; flex-direction: row;}
.cMyBox {width: 20px; height: 20px; background-color: blue;}
.cWhite{background-color: white;}
.cColor1 {background-color: red;}
.cColor2 {background-color: blue;}
.cColor3 {background-color: green;}
当它不改变颜色时,它是假的。我也试过ng-class="{cWhite: item.Properties.P1 == false}"。
我用这种方式添加了几次带有 ng-class + 表达式的类,但现在它不起作用。也许我的重复或我的列表是错误的。
谢谢!
【问题讨论】:
标签: html angularjs ng-repeat ng-class