angularjs ng-class not working when expression = array with object and string

夙愿已清 提交于 2019-12-10 23:36:25

问题


I don't know if it's now for the sake of it, but i am stuck with a statement in the AngularJS documentation:

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

"... If the expression evaluates to an array, each element of the array should either be a string as in type 1 or an object as in type 2. This means that you can mix strings and objects together in an array to give you more control over what CSS classes appear..."

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

If you see my example on http://jsfiddle.net/angelinena/9p4k4wnx/5/ it works in all combinations, but not as an array with object and string in it.

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

Could you please tell me what i am doing wrong? Would be very grateful.


回答1:


With array of object you need to evaluate the expression on your own (If you are not using angular 1.4+), Assuming warning holds true value, you could do.

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

Or just use a mix of ng-class and class. i.e

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

You might be referring to the latest documentation (1.4+) and using an older version of angular. You can either upgrade the version of angular to atleast 1.4 or manually evaluate the expression.

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.



来源:https://stackoverflow.com/questions/33481415/angularjs-ng-class-not-working-when-expression-array-with-object-and-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!