简体   繁体   English

获取由内容元素选择的dom-repeat模板生成的元素

[英]get elements produced by a dom-repeat template selected by a content element

i'm trying to get elements produced by a helper elements on polymer, which is selected of a content tag. 我正在尝试通过聚合物上的辅助元素生成元素,聚合物选择内容标记。 this a simple example that I trying to do: 这是我尝试做的一个简单示例:

dom-module DOM模块

 <dom-module id="x-foo"> <template> <content id="content"></content> </template> <script> Polymer({ is: 'x-foo', attached: function() var effectiveChildren = this.getEffectiveChildren("span"); console.log(effectiveChildren); } }); </script> </dom-module> 

index.html 的index.html

 <template is="dom-bind"> <x-foo> <template is="dom-repeat" items="{{list}}"> <span>{{item.name}}</span> </template> </x-foo> </template> <script> var scope = document.querySelector("template[is='dom-bind']"); scope.list = [{"name":"javier"},{"name":"pedro"},{"name":"javier2"}] </script> 

--- edit --- ---编辑---

I try with: 我试着用:

getDistributedNodes()
getEffectiveChildren()
getContentChildren()

any hint?? 任何提示?

So there was a little bit more to this, but also a little bit less. 所以对此有一点点,但也少一点。 The code you looking for is: 您要查找的代码是:

this.queryAllEffectiveChildren('span');

However, there's gonna be a lot of particularities around when all of the DOM is available to actually run this depending on the final application of your component. 但是,根据组件的最终应用程序,当所有DOM都可用于实际运行时,会有很多特殊性。 I've got it working here: http://plnkr.co/edit/nfER4vTe5y7CddHYO5bk?p=preview the main thing being I'm waiting for the dom-change event on the dom-repeat to check for those elements. 我已经在这里工作: http//plnkr.co/edit/nfER4vTe5y7CddHYO5bk?p=preview主要的是我正在等待dom-repeat上的dom-repeat dom-change事件来检查这些元素。 There's definitely something cleaner to be used around DOM flush or the like, but I'm not sure which way to direct you in there. 肯定有一些更清洁的东西可以用于DOM冲洗等,但我不知道哪种方式可以指导你。 The main crux is: 主要症结是:

<template>
  There are {{childCount}} children.<br/>
    <content></content>
</template>
<script>
    Polymer({
        is: 'my-element',
        listeners: {
          'dom-change': 'childCheck'
        },
          childCheck: function() {
              this.childCount = this.queryAllEffectiveChildren('span').length;
              console.log(this.queryAllEffectiveChildren('span'));
        }
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM