简体   繁体   English

ngStyle 不适用于 !important in angular2

[英]ngStyle does not work with !important in angular2

In angular2 template if I write:在 angular2 模板中,如果我写:

[ngStyle]="{'background-color': 'red'}"

it works as expected.它按预期工作。 But If I write:但如果我写:

[ngStyle]="{'background-color': 'red !important'}"

it's not working (not showing any error also)它不起作用(也没有显示任何错误)

As angular2 documentation is still not good enough, I think it may be helpful to post the solution as an answer ( may be it helps someone ).由于 angular2 文档仍然不够好,我认为将解决方案作为答案发布可能会有所帮助(可能对某人有所帮助)。

According to angularjs (1.x) git repository issue根据 angularjs (1.x) git repository issue

You're not able to use the !important directive in the DOM style property in either Chrome nor FF (probably others too)您不能在 Chrome 或 FF 的 DOM 样式属性中使用 !important 指令(也可能是其他)

So instead of using !important in [ngStyle] I use the following way to make it work:因此,我没有在 [ngStyle] 中使用 !important,而是使用以下方法使其工作:

[attr.style]="'background-color:red !important'"

It works as expected.它按预期工作。

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

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