当expression =包含对象和字符串的数组时,angularjs ng-class无法正常工作 [英] angularjs ng-class not working when expression = array with object and string

查看:100
本文介绍了当expression =包含对象和字符串的数组时,angularjs ng-class无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道现在是不是因为它,但是我在AngularJS文档中停留了一条语句:

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

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

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

推荐答案

使用对象数组,您需要自己评估表达式(如果不使用angular 1.4 + ),则假定warning可以保存true值.

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

或者只是混合使用ng-class和class.即

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

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

您可能会参考最新的文档(1.4+),并使用了较旧的angular版本.您可以将angular的版本升级到至少1.4,也可以手动评估表达式.

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.

旧文档状态:

如果表达式的计算结果为数组,则数组的每个元素应为一个字符串,该字符串是一个或多个以空格分隔的类名.

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.

这篇关于当expression =包含对象和字符串的数组时,angularjs ng-class无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆