简体   繁体   English

聚合物0.8:HTML标记复杂属性内的数据绑定

[英]Polymer 0.8: Data binding inside a HTML tag complex attribute

I understood that in the newer Polymer release 0.8 binding to an attribute inside a tag should be followed after $ sign. 我了解到,在较新的Polymer版本0.8中,应在$符号后跟随对标签内属性的绑定。 And it works like so: 它的工作原理如下:

<tag attribute$="{{DATA}}">

eg this works great: 例如,这很好用:

href$="{{url}}"

But it doesn't work when the attribute gets more complex, eg in my code example: 但是,当属性变得更加复杂时,它就无法工作,例如在我的代码示例中:

<a style$="background-image: url({{backgroundimage}});">

which is a String data attribute. 这是一个字符串数据属性。

Is it supported in 0.8 like it was in 0.5 ? 像0.5一样支持0.8吗?

Complex bindings like that are not currently supported in 0.8. 0.8当前不支持类似的复杂绑定。 Instead you'd probably want to create a computed property and use that. 相反,您可能想创建一个计算属性并使用它。

Btw, you only need to use $={{ }} syntax if you need work with an attribute that doesn't have a corresponding property on an element. 顺便说一句,如果您需要使用在元素上没有对应属性的属性,则只需要使用$={{ }}语法。 Because HTMLAnchorElement (the <a> tag) has an href property, you don't have to use a dollar sign in that case, you can just create a regular binding using href={{url}} . 由于HTMLAnchorElement( <a>标记)具有href属性,因此在这种情况下您不必使用美元符号,只需使用href={{url}}即可创建常规绑定。

To put it another way: 换一种方式:

href="{{site}}" means "bind this.site to element.href" href$="{{site}}" means "bind this.site to element.attributes.href.value" href =“ {{site}}”的意思是“将该this.site绑定到element.href” href $ =“ {{site}}”的意思是“将该this.site绑定到element.attributes.href.value”

You usually only need the $ syntax when working with boolean attributes like hidden or disabled. 使用隐藏或禁用的布尔属性时,通常只需要$语法。 Or in the case where an element does not have a corresponding property in JavaScript. 或在JavaScript中元素没有相应属性的情况下。

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

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