如何将数据从JavaScript代码传递到Polymer自定义元素?

jor*_*gos 6 javascript custom-component polymer

我想通过属性将我在变量上的数据传递给Polymer组件.

这是代码:

<script>
  var item1 = {
      title: "Title 1",
      status: "accepted"
  };
</script>

<bm-card item="{{item1}}" otherAttribute="hello">
Run Code Online (Sandbox Code Playgroud)

otherAttribute获取自定义元素上的数据,但项目不到货.

如何从item1变量填充item属性?

ebi*_*del 4

<polymer-element>要在您需要之外使用数据绑定<template is="auto-binding">https://www.polymer-project.org/docs/polymer/databinding-advanced.html#bindingoutside

不过,你可以直接在js中设置属性:

<bm-card otherAttribute="hello">

document.addEventListener('polymer-ready', function() {
  document.querySelector('bm-card').item = item1;
});
Run Code Online (Sandbox Code Playgroud)

您必须等待聚合物就绪的原因是为了确保元素已在 DOM 中升级并定义了其属性/方法。