简体   繁体   English

如何在Polymer 1.0中的html中修改布尔属性值?

[英]How to modify boolean property value in html in polymer 1.0?

If I assign a boolean property to a component, how can I modify the value of the boolean in html, without creating any function in javascript? 如果将布尔属性分配给组件,如何在html中修改布尔值,而无需在javascript中创建任何函数?

For example, lets say I have the following component: 例如,假设我具有以下组件:

 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> 

How can I make fixed-position true or false within the html? 如何在html中使fixed-position正确或错误?

Thanks 谢谢

Boolean properties works as normal boolean attrribute in HTML, example: 布尔属性像HTML中的常规布尔属性一样,例如:

<input type="checkbox" checked />
<input type="text" disabled />

To swtich ON/OFF you need to do remove or add the attribute, then 要打开/关闭,您需要删除或添加属性,然后

  • Switch on: <find-retailer-map fixed-position></find-retailer-map> 开启: <find-retailer-map fixed-position></find-retailer-map>
  • Swtich off: <find-retailer-map></find-retailer-map> 关闭: <find-retailer-map></find-retailer-map>

To swtich ON/OFF programamtically: 以编程方式打开/关闭:

  • Switch on: <find-retailer-map fixed-position$="{{_your_toggle(true)}}"></find-retailer-map> 打开: <find-retailer-map fixed-position$="{{_your_toggle(true)}}"></find-retailer-map>
  • Switch off: <find-retailer-map fixed-position$="{{_your_toggle(false)}}"></find-retailer-map> 关闭: <find-retailer-map fixed-position$="{{_your_toggle(false)}}"></find-retailer-map>

hope that helps 希望能有所帮助

Booleans in HTML and Polymer works the same. HTML和Polymer中的布尔值的作用相同。 Either they are there (true), or they are not (false). 它们要么在(true),要么不在(false)。 These three are doing the same thing ... 这三个人在做同一件事...

<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>

... as you can pass in anything in the fixed-position attribute to set it true. ...因为您可以在fixed-position属性中传递任何内容以将其设置为true。 To set it false, however, you need to remove the attribute. 要将其设置为false,则需要删除该属性。

<find-retailer-map></find-retailer-map>

If you want to set the property dynamically, pass in a variable, as an attribute, from osb-retailer-page into find-retailer map. 如果要动态设置属性,请将变量作为属性从osb-retailer-page传递到find-retailer映射中。

<dom-module id="osb-retailer-page">
  <template>
    <find-retailer-map fixed-position="[[aVariableInRetailerPage]]"></find-retailer-map>
  </template>
</dom-module>

However, the default value for the fixedPosition property in find-retailer-map must be false (or not set, just like your example). 但是,find-retailer-map中的fixedPosition属性的默认值必须为false(或未设置,就像您的示例一样)。 You cannot change that property if it defaults to true. 如果该属性默认为true,则无法更改。

Boolean properties are set based on the presence of the attribute: if the attribute exists at all, the property is set to true, regardless of the attribute value. 根据属性的存在来设置布尔属性:如果属性完全存在,则将属性设置为true,而与属性值无关。 If the attribute is absent, the property gets its default value. 如果该属性不存在,则该属性将获得其默认值。 /.../ For a Boolean property to be configurable from markup, it must default to false. /.../要使布尔属性可通过标记配置,则必须默认为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. 如果默认设置为true,则无法从标记将其设置为false,因为属性的存在(带有或不带有值)都等于true。 This is the standard behavior for attributes in the web platform. 这是Web平台中属性的标准行为。

https://www.polymer-project.org/1.0/docs/devguide/properties https://www.polymer-project.org/1.0/docs/devguide/properties

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

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