如何在 Polymer 1.0 中创建一个新的模板实例?

How to create a new template instance in Polymer 1.0?

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

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

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

如果您这样定义模板:

<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 元素时,这或多或少会发生这种情况,除了呈现器是您的元素的实例(而不是匿名对象)。