2

在 Polymer 0.5 中,我使用 templateElement.createInstance(dataToBind) 创建模板的新实例并将数据对象绑定到新实例:

var instance = templateElement.createInstance(dataToBind);
container.appendChild(instance);

在 Polymer.Base 中我找到了 instanceTemplate 函数来创建模板的新实例,但该函数不会将数据绑定到实例。有没有办法在 Polymer 1.0 中实现这一点?

4

1 回答 1

4

如果您定义这样的模板:

<template is="dom-template">
  <h2>{{name}}</h2>
  <h3>...lives<h3>
</template>

您可以生成这样的实例:

<script>
  // construct the anonymous presenter
  var instance = templateInstance.stamp();
  // the data model lives on the presenter
  instance.name = 'Instance';
  // the nodes are available in `root`
  document.body.appendChild(instance.root);
</script>

这或多或少是在您实例化 Polymer 元素时发生的情况,除了 Presenter 是您的元素的实例(而不是匿名对象)。

于 2015-06-12T01:45:01.537 回答