如何用 Polymer 中的属性覆盖 属性 的 value:true
How to override property's value:true with an attribute in Polymer
在聚合物中,如果我有一个名为 value
的 属性,类型为 Boolean
,默认值为 true
:
static get properties() {
return {
value: {
type: Boolean,
value: true
}
}
}
有什么方法可以使用 HTML 属性将值设置为 false
吗?
例如所有这些样本都将 value
属性 解释为 true
:
<my-element></my-element>
<my-element value></my-element>
<my-element value="false"></my-element>
<my-element value="[[false]]"></my-element>
<my-element value="{{false}}"></my-element>
<my-element value="{{!constructor}}"></my-element>
这是一个代码笔示例:http://codepen.io/anon/pen/KWdWRm
不,很遗憾,您不能将 false
值绑定到布尔值 属性,正如 Polymer docs 解释的那样:
For a Boolean property to be configurable from markup, it must default to false
. If it defaults to true
, you cannot set it to false
from markup, since the presence of the attribute, with or without a value, equates to true
. This is the standard behavior for attributes in the web platform.
If this behavior doesn't fit your use case, you can use a string-valued or number-valued attribute instead.
在聚合物中,如果我有一个名为 value
的 属性,类型为 Boolean
,默认值为 true
:
static get properties() {
return {
value: {
type: Boolean,
value: true
}
}
}
有什么方法可以使用 HTML 属性将值设置为 false
吗?
例如所有这些样本都将 value
属性 解释为 true
:
<my-element></my-element>
<my-element value></my-element>
<my-element value="false"></my-element>
<my-element value="[[false]]"></my-element>
<my-element value="{{false}}"></my-element>
<my-element value="{{!constructor}}"></my-element>
这是一个代码笔示例:http://codepen.io/anon/pen/KWdWRm
不,很遗憾,您不能将 false
值绑定到布尔值 属性,正如 Polymer docs 解释的那样:
For a Boolean property to be configurable from markup, it must default to
false
. If it defaults totrue
, you cannot set it tofalse
from markup, since the presence of the attribute, with or without a value, equates totrue
. This is the standard behavior for attributes in the web platform.If this behavior doesn't fit your use case, you can use a string-valued or number-valued attribute instead.