Polymer代码中HTML属性名内$符号的含义咨询
$ Syntax in Polymer Attribute Binding Hey there! Let me break down that $ syntax you encountered in your Polymer code—it's a purpose-built feature for handling boolean attribute binding, and it's super useful once you get the hang of it.
What does the $ do?
In Polymer, when you write an attribute like wide-layout$="{{wide}}", the $ tells the framework that this is a boolean attribute binding. Here's how it works:
- When the bound variable (
widein your example) evaluates totrue(or any truthy value), thewide-layoutattribute will be added to the element (you'll see<div class="item" wide-layout>in the DOM). - When the variable is
false(or falsy, likenull/undefined), thewide-layoutattribute will be removed entirely from the element.
Why this matters for boolean attributes
HTML boolean attributes work differently than regular attributes—their presence alone is enough to trigger their behavior (think <input disabled> or <button checked>). A regular attribute binding like wide-layout="{{wide}}" would set the attribute value to the string "true" or "false", which isn't what you want for boolean attributes. The $ syntax fixes this by directly controlling whether the attribute exists at all, aligning perfectly with how HTML treats these attributes.
Your example in action
Let's tie this to your code:
- When
wideistrue, the element has thewide-layoutattribute, so your CSS selector.item[wide-layout] .titlewill target the.titlechild and apply its styles. - When
wideisfalse, the attribute is gone, so that CSS selector won't match anything, and the styles won't be applied.
This is a clean way to toggle styles or component behavior based on a boolean state without having to manually add/remove attributes in your JavaScript.
内容的提问来源于stack exchange,提问作者jldupont

