Polymer 1.0将属性绑定到模板中的内联样式

Polymer 1.0将属性绑定到模板中的内联样式,polymer,polymer-1.0,Polymer,Polymer 1.0,我想在聚合物中做类似的事情 <dom-module id="logo-standard"> <style> :host { display: block; } </style> <template> <div class="logo-wrap"> <div style="width: {{logoWidth}}px;"> Some awesome logo </div> </t

我想在聚合物中做类似的事情

<dom-module id="logo-standard">
 <style>
:host {
  display: block;
}
</style>

<template>

<div class="logo-wrap">

  <div style="width: {{logoWidth}}px;">
    Some awesome logo
  </div>

</template>

<script>

(function() {

Polymer({
  is: 'logo-standard',

  properties: {

    logoWidth: {
      type: String,
      value: '400'
    }
  }

});
})();
</script>
</dom-module>

:主持人{
显示:块;
}
一些很棒的标志
(功能(){
聚合物({
是:‘标志标准’,
特性:{
标识宽度:{
类型:字符串,
值:“400”
}
}
});
})();
i、 e.使用属性动态设置元素样式


这可能吗?如果是的话。。。如何更改?

在Polymer 1.0中,他们更改了一些不允许您这样内联样式的内容。您必须使用computed styles函数,并让它返回所需的值

 <div style$="{{computeWidth}}">



 computeWidth: function () {
   return 'width:' + this.logoWidth + 'px;'
 }

computeWidth:函数(){
返回'width:'+this.logoWidth+'px;'
}
这是另一篇关于这个主题的帖子


编辑:我忘记了Polymer 1.0中的$

,您需要这样做-

  <div style$="[[_computeWidth(logoWidth)]]">
    Some awesome logo
  </div>

  _computeWidth: function (width) {
      return 'width: ' + width + 'px' + ';color:red;';
  },

一些很棒的标志
_computeWidth:函数(宽度){
返回'width:'+width+'px'+';颜色:红色;';
},

请参见此以供参考。

是的,但您需要为此创建计算绑定:

<dom-module id="logo-standard">
    <style>
        :host {
            display : block;
        }
    </style>
    <template>
        <div class="logo-wrap">
            <div style$="[[logoStyle(logoWidth)]]">
                Some awesome logo
            </div>
    </template>
    <script>
        Polymer({
            is : 'logo-standard',

            properties : {

                logoWidth : {
                    type  : String,
                    value : '400'
                }
            },
            logoStyle  : function (logoWidth) {
                return 'width: ' + logoWidth + 'px';
            }
        });
    </script>
</dom-module>

:主持人{
显示:块;
}
一些很棒的标志
聚合物({
是:‘标志标准’,
特性:{
标识宽度:{
类型:字符串,
值:“400”
}
},
logoStyle:函数(logoWidth){
返回'width:'+logoWidth+'px';
}
});

解决后,可以不使用计算绑定,这似乎正在进行中。

我也回答了这个问题

截至,您现在可以使用

在单个属性绑定或文本内容绑定中组合字符串文字和绑定

像这样:


姓名:{{lastname},{{firstname}
你的例子呢



所以这不再是一个问题

请注意,
style$=
将值绑定到属性,而不仅仅是元素属性我已经使用1.2一段时间了,但不知道这一点。。。非常有用,谢谢