Polymer 聚合物1.0模板绑定参考等效物

Polymer 聚合物1.0模板绑定参考等效物,polymer,Polymer,在聚合物0.5中,可以执行以下操作: <template bind ref="howdyTpl"></template> ... <template id="howdyTpl">Howdy {{whatever}}</template> ... 你好{{随便什么} id为“howdyTpl”的模板将在ref属性中引用的位置加盖戳记 如何在Polymer 1.0中执行类似操作?这里有一个自定义元素,可以执行类似操作 Polymer({ is

在聚合物0.5中,可以执行以下操作:

<template bind ref="howdyTpl"></template>
...
<template id="howdyTpl">Howdy {{whatever}}</template>

...
你好{{随便什么}
id为“howdyTpl”的模板将在
ref
属性中引用的位置加盖戳记


如何在Polymer 1.0中执行类似操作?

这里有一个自定义元素,可以执行类似操作

Polymer({
  is: 'bind-ref',

  properties: {
    ref: {
      type: String,
      observer: 'refChanged',
    },
  },

  refChanged: function(newRef, oldRef) {
    if (oldRef != undefined) {
      this._removeChildren();
      this._stamp();
    }
  },

  _stamp: function() {
    this._parent = Polymer.dom(this).parentNode;
    var rootEl = this._parent;
    while (Polymer.dom(rootEl).parentNode) {
      rootEl = Polymer.dom(rootEl).parentNode;
    }

    var tpl = Polymer.dom(rootEl).querySelector('#' + this.ref);
    var tplRoot = tpl.stamp().root;
    this._children = Array.prototype.slice.call(tplRoot.childNodes);
    Polymer.dom(this._parent).insertBefore(tplRoot, this);
  },

  _removeChildren: function() {
    for (var i = 0; i < this._children.length; i++) {
      Polymer.dom(this._parent).removeChild(this._children[i]);
    }
  },

  attached: function() { this._stamp(); },
  detached: function() { this._removeChildren(); },
});

(代码片段已获得许可。)

这是一种黑客行为,但它可以。。。包括多深度文件包含和数据绑定,但另一个答案没有处理。

遗憾的是,当bind ref和template都是同级时,这种方法可以工作。如果bind ref在中,则不起作用。@miyconst它在
dom repeat
中对我起作用。你看到了什么问题?另外,我刚刚修复了它,这样它在分离时就可以删除标记元素。我正在尝试制作一个自定义数据网格组件。用户应该能够为列定义自定义模板。这里有草稿工作:Actions列有一个使用bind ref的模板,但绑定在内部不起作用。
<bind-ref ref="howdyTpl"></bind-ref>
<template is="dom-template" id="howdyTpl">Howdy <span>{{whatever}}</span></template>