简体   繁体   English

解析背景色的值时出错-angularjs

[英]Error in parsing value for background color - angularjs

Below code, 下面的代码,

<div style="background-color: {{ row.legend }}"></div>

executes fine, 执行得很好,

but komodo editor shows the error as Error in parsing value for 'background-color' 但komodo编辑器将错误显示Error in parsing value for 'background-color'

I tried modifying to: ng-style="'background-color': '{{ row.legend }}'" , but functionality breaks. 我尝试修改为: ng-style="'background-color': '{{ row.legend }}'" ,但是功能中断。

How do I resolve this error? 如何解决此错误?

Always use ng-style over style with evaluation braces. 始终使用带评估括号的ng-style over样式。

The technical background is that some browsers will remove invalid style properties from the DOM immediately hence angular will not be able to update the style property later on even if they would be valid by then. 技术背景是某些浏览器会立即从DOM中删除无效的样式属性,因此angular以后将无法更新样式属性,即使那时它们将是有效的。

In your example 在你的例子中

<div style="background-color: {{ row.legend }}"></div>

may evaluate to 可能会评估为

<div style="background-color: "></div>

in the first place and thus be removed from DOM. 首先,因此从DOM中删除。 If row.legend takes a valid value afterwards angular won't be able to update the DOM accordingly. 如果row.legend之后使用有效值,则angular将无法相应地更新DOM。

This will not happen with the built-in directive ng-style. 内置指令ng-style不会发生这种情况。

For completeness i will repeat the answer of @overexchange 为了完整性,我将重复@overexchange的答案

The problem in the latter case are the missing braces, so the solution would be 后一种情况的问题是缺少括号,因此解决方案是

<div ng-style="{'background-color': row.legend }"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM