【问题标题】:Polymer - Conditional Template聚合物 - 条件模板
【发布时间】:2015-10-27 11:43:33
【问题描述】:

我正在学习 Polymer。现在,我正在尝试有条件地显示一个模板。

my-component.html

<dom-module id="my-component">
    <template>
      <h5>There are <span>[[ orders.length ]]</span> orders.</h5>
      <template is="dom-if" if="[[ orders.length > 0]]">
        <template is="dom-repeat" items="{{ orders }}" as="order">
          <div class="order-item">
            <span>[[ order.description ]]</span>
          </span>              
        </template>        
      </template>

      <template is="dom-if" if="[[ orders.length == 0]]">
        No orders have been placed
      </template>
    </template>

    <script>
        Polymer({
            is: "my-component",
            properties: {
              orders: Array
            }                    
        });
  </script> 
</dom-module>

我的问题是,如果我的数组中有项目,我如何显示一个 HTML 块,如果数组没有任何项目,我如何显示另一个 HTML 块? h5 标记显示正确的项目数。因此,我知道我的绑定设置正确。但是,我不知道如何有条件地显示模板。

谢谢!

【问题讨论】:

    标签: polymer


    【解决方案1】:

    Polymer 不支持那种expressions in binding。一般来说,如果你想要表达式,你需要使用computed bidings来代替。在这种特定情况下,因为 0 计算为 false 而其他值计算为 true,你可以这样做:

      <template is="dom-if" if="[[ orders.length ]]">
        <template is="dom-repeat" items="{{ orders }}" as="order">
          <div class="order-item">
            <span>[[ order.description ]]</span>
          </div>              
        </template>        
      </template>
    
      <template is="dom-if" if="[[ !orders.length ]]">
        No orders have been placed
      </template>
    

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多