【问题标题】:Polymer, template won't evaluate true and bind聚合物,模板不会评估真实和绑定
【发布时间】:2015-02-01 08:23:48
【问题描述】:

使用聚合物我非常简单地尝试评估对象属性是否为特定值,如果是,则将真实模板的内容绑定到 dom。

我有:

<polymer-element name="template-elem">
  <template >
    <div>
    <template if={{response.data.type === "message"}}>
      <div>working</div>
    </template>
    </div>
  </template>
  <script>

    Polymer({
      response: {}, 
        ready: function(){
      this.response = {"data":{
        "type":"message",
        "detail":"default"
      }};
        },
    });
  </script>
</polymer-element>

也试过了,==“消息”。如果要求计算布尔表达式,模板会绑定,{{response.data.detail}} 会显示正确的文本。我已经在两个单独的元素中尝试过这个(从头开始)并且问题仍然存在,谁能告诉我我明显忽略了什么?

还尝试传递给辅助函数{{response | parseObj}}, {{item in response | parseObj}}, {{item in response.data | parseObj}},但该函数永远不会被调用。

【问题讨论】:

  • 传递一个函数的时候,你真的调用了这个函数吗? {{fnname()}} 否则只检查函数是否存在。
  • 根据聚合物docs ,功能执行不需要( )。它应该看到附加到这个(元素)的函数,| 运算符应该将 arg 传递到左侧。例如"{{item | parseObj}}"
  • 根据polymer-project.org/docs/polymer/expressions.html,有些表达式需要返回值才能进行评估。也就是说,我很高兴@Dirk Grappendorf 的回答解决了它!
  • 哦,好吧,所以你也有这个选项。感谢您的帮助!

标签: javascript html templates polymer web-component


【解决方案1】:

您忘记了 if 属性值的引号。试试

<template if="{{response.data.type === 'message'}}">

如果没有引号,浏览器 (Chrome) 会将此行解析为

<template if="{{response.data.type" =="=" 'message'}}=""></template>

【讨论】:

  • Annnnd 这就是问题所在,猜猜我太累了会发生什么!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多