gpt4 book ai didi

javascript - 当expression = array with object and string时angularjs ng-class不工作

转载 作者:行者123 更新时间:2023-11-29 21:39:43 25 4
gpt4 key购买 nike

我不知道现在是不是为了它,但我坚持使用 AngularJS 文档中的声明:

https://docs.angularjs.org/api/ng/directive/ngClass

"... 如果表达式的计算结果为数组,则数组的每个元素应该是类型 1 中的字符串或类型 2 中的对象。这意味着您可以在数组中将字符串和对象混合在一起让您更好地控制显示哪些 CSS 类...”

"...<p ng-class="[style4, {orange: warning}]">Using Array and Map Syntax</p>..."

如果你在 http://jsfiddle.net/angelinena/9p4k4wnx/5/ 上看到我的例子它适用于所有组合,但不适用于其中包含对象和字符串的数组。

<p><button class="btn btn-default" ng-click="conditionlead=true; classname='text-info'">Both 2</button></p>
<p ng-class="[{ 'lead': conditionlead == true }, classname ]">Classes applied as array with object and string</p>

你能告诉我我做错了什么吗?将不胜感激。

最佳答案

对于对象数组,您需要自行评估表达式(如果您未使用 Angular 1.4+),假设 warning 成立 true 值,你可以做到。

 ng-class="[style4, {true: 'orange'}[warning]]"

或者混合使用 ng-class 和 class。即

 <p class="{{style4}}" ng-class="{orange: warning}">

您可能指的是最新的文档 (1.4+) 并使用旧版本的 angular。您可以将 Angular 的版本升级到至少 1.4 或手动评估表达式。

older documentation states :

If the expression evaluates to an array, each element of the array should be a string that is one or more space-delimited class names.

关于javascript - 当expression = array with object and string时angularjs ng-class不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33481415/

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