简体   繁体   English

如何强制ng-style添加到现有样式?

[英]How do I force ng-style to ADD to an existing style?

I have a custom popover directive and another custom directive that uses that one. 我有一个自定义popover指令和另一个使用该指令的自定义指令。 I'm trying to use ng-style to set the width of the popover. 我正在尝试使用ng-style来设置popover的宽度。 Here's a code snippet from the directive's html template: 这是指令的html模板中的代码片段:

<div my-custom-popover ng-style="{'width': widthVar}">
    ...
</div>

Here's where it gets interesting: my-custom-popover also has an ng-style applied. 这里有趣的地方: my-custom-popover也应用了ng-style。 Here's some code from my-custom-popover 's html template: 这是my-custom-popover的html模板中的一些代码:

<div ng-style="{display: (condition ? 'block' : 'none')}">
    ...
</div>

The problem is that when angular tries to render my directive, rather than being smart and simply adding my ng-style to the existing one, it simply concatenates them and results in a parsing error. 问题是当angular试图渲染我的指令时,而不是聪明并且只是将我的ng样式添加到现有指令中,它只是连接它们并导致解析错误。 By the time the browser sees my markup it looks like ng-style={'width': widthVar} {display: (condition ? 'block' 'none')} which is obviously incorrect. 当浏览器看到我的标记时,它看起来像ng-style={'width': widthVar} {display: (condition ? 'block' 'none')}这显然是不正确的。

Is there anyway to tell angular that my ng-style should be appended to the existing one and not just concatenated like is being done? 无论如何要告诉棱角度我的ng风格应该附加到现有的风格,而不仅仅是像正在进行的那样连接?

In general, it is not a good idea to use inline CSS rules because if you have several of the same rules in different elements and need to change them, you have to do so in each element which can be a hassle and you may miss one. 一般来说,使用内联CSS规则并不是一个好主意,因为如果你在不同的元素中有几个相同的规则并且需要更改它们,你必须在每个元素中这样做,这可能是一个麻烦,你可能会错过一个。 Instead, I would just import a css file so that the rules can be easily changed. 相反,我只需导入一个css文件,以便可以轻松更改规则。

Having said that, I would use ng-class in place of ng-style in your directive and apply CSS rules to the particular classes. 话虽如此,我会在你的指令中使用ng-class代替ng-style,并将CSS规则应用于特定的类。

For example, you can replace 例如,您可以替换

ng-style="{display: (condition ? 'block' : 'none')}"

with

ng-class="{'show-div': show, 'hide-div': !show}"

and then create a css file and apply the following rules: 然后创建一个css文件并应用以下规则:

.show-div{display:block;}
.hide-div{display:none;}

Apparently, it's currently not possible to merge attributes: read this open issue . 显然,目前无法合并属性: 阅读本期未解决的问题

Alternatively, you might consider using the following suggested workaround on your custom directive: 或者,您可以考虑在自定义指令上使用以下建议的解决方法

compile: function compile(tElement, tAttrs, transclude) {
  tAttrs.ngClass = tAttrs.ngClass.replace(/}\s*{/g, ', ');
  // ...
}

It then fixes the incorrect {'width': widthVar} {display: (condition ? 'block' 'none')} to {'width': widthVar, display: (condition ? 'block' 'none')} by replacing } { with , before it is compiled. 然后通过{'width': widthVar} {display: (condition ? 'block' 'none')}将不正确的{'width': widthVar} {display: (condition ? 'block' 'none')} {'width': widthVar, display: (condition ? 'block' 'none')}{'width': widthVar, display: (condition ? 'block' 'none')} } { with ,在编译之前。

See also dariocravero 's fiddle. 另见dariocravero的小提琴。

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

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