如果我给一个组件分配一个布尔型属性,我如何在html中修改布尔值,而不用在javascript中创建任何函数呢?
例如,假设我有以下组件:
Polymer({
is: 'find-retailer-map',
properties: {
fixedPosition: {
type: Boolean,
notify: true,
},
},
});<dom-module id="osb-retailer-page">
<template>
<find-retailer-map fixed-position></find-retailer-map>
</template>
</dom-module>
如何在html中使fixed-position为真或假?
谢谢
发布于 2017-07-08 00:37:41
在HTML中,布尔型属性的作用与普通布尔型属性类似,例如:
<input type="checkbox" checked />
<input type="text" disabled />要打开/关闭swtich,您需要删除或添加属性,然后
<find-retailer-map fixed-position></find-retailer-map>
<find-retailer-map></find-retailer-map>:
要通过编程打开/关闭,请执行以下操作:
<find-retailer-map fixed-position$="{{_your_toggle(true)}}"></find-retailer-map> <find-retailer-map fixed-position$="{{_your_toggle(false)}}"></find-retailer-map> 希望这能有所帮助
发布于 2017-07-08 15:44:42
HTML中的布尔值和聚合物中的布尔值是相同的。它们要么存在(true),要么不存在(false)。这三个人在做同样的事情。
<find-retailer-map fixed-position></find-retailer-map>
<find-retailer-map fixed-position="true"></find-retailer-map>
<find-retailer-map fixed-position="false"></find-retailer-map>..。因为您可以传入固定位置属性中的任何内容以将其设置为true。但是,要将其设置为false,您需要删除该属性。
<find-retailer-map></find-retailer-map>如果希望动态设置该属性,请将一个变量作为属性从osb-retailer-page传递到find-retailer map。
<dom-module id="osb-retailer-page">
<template>
<find-retailer-map fixed-position="[[aVariableInRetailerPage]]"></find-retailer-map>
</template>
</dom-module>但是,find-retailer-map中的fixedPosition属性的默认值必须为false (或者没有设置,就像您的示例一样)。如果该属性默认为true,则不能更改该属性。
布尔属性是基于属性的存在而设置的:如果该属性存在,则无论属性值如何,该属性都将设置为true。如果该属性不存在,则该属性将获取其默认值。/.../对于可从标记中配置的布尔值属性,它必须缺省为false。如果它缺省为true,则不能从标记中将其设置为false,因为该属性的存在,无论是否有值,都等同于true。这是web平台中属性的标准行为。
https://www.polymer-project.org/1.0/docs/devguide/properties
https://stackoverflow.com/questions/44967308
复制相似问题