我想知道如何制作False ng-class。
如果页面被选中,page.isSelected(1)为TRUE,否则为FALSE
<div id="homePage" ng-class="{ center: page.isSelected(1) }">
因此我想要你:
isSelected为TRUE:center
isSelected为FALSE:left
我试过了:
<div id="homePage" ng-class="{ page.isSelected(1) 'center : 'left' }">
但它不起作用。
我不知道我做错了什么。你能帮帮我吗?
答案 0 :(得分:63)
为每个案例制定一个规则:
<div id="homePage" ng-class="{ 'center': page.isSelected(1) , 'left': !page.isSelected(1) }">
或者使用三元运算符:
<div id="homePage" ng-class="page.isSelected(1) ? 'center' : 'left'">
答案 1 :(得分:27)
您可以使用三元运算符表示法:
<div id="homePage" ng-class="page.isSelected(1)? 'center' : 'left'">
答案 2 :(得分:9)
John Conde's和ryeballar's个答案都是正确无误的。
如果你想太讨厌:
John的缺点是每个$ digest循环必须做出两个决定(它必须决定是否添加/删除center
并且必须决定是否添加/删除{{1} }),显然只需要一个。
Ryeballar依赖于三元运算符,它可能会在某些时候被删除(因为视图不应该包含任何逻辑)。 (我们无法确定它是否会被删除,它可能不会很快就会消失,但如果有更“安全”的解决方案,为什么不呢?)
因此,您可以选择执行以下操作:
left