gpt4 book ai didi

javascript - ng-Class - 多个条件和类

转载 作者:搜寻专家 更新时间:2023-11-01 05:11:11 26 4
gpt4 key购买 nike

我一直在 ng-class 中寻找多个条件的示例,但找不到使用相同术语的示例。这是我想要实现的目标的示例。

ng-class="isSelected ? 'side_menu_link_active' : 'side_menu_link_disabled' , pageSelected =='lectures' ? 'link_active' : 'link_disabled'"

有人知道正确的语法吗?谢谢。

最佳答案

如果你想要多个条件,语法是:

ng-class=" {className: valueToCheckForTruthiness, otherClassName: otherValue  } " 

使用类名的示例:

ng-class="{side_menu_link_active: isSelected, side_menu_link_disabled: !isSelected, link_active: pageSelected == 'lectures', link_disabled: pageSelected != 'lectures'   }"

您可以更进一步,使用函数生成一个对象:

$scope.classGenerator = function(isSelected, pageSelected){
var obj = {};
isSelected ? obj.side_menu_link_active=true : obj.side_menu_link_disabled = false;
pageSelected == 'lectures' ? obj.link_active = true : obj.link_disabled = false;
return obj;
}
ng-class="classGenerator(isSelected, pageSelected)"

关于javascript - ng-Class - 多个条件和类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28199257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com