javascript - 聚合物 2 查询选择器不起作用

标签 javascript polymer-2.x dom-repeat

我尝试向一些动态创建的元素添加事件监听器,但我无法定位它们。

//here is how I try to access it
_buildTable(data)
{
    this.$.spinner.style.display = 'none';
    this.tableHead = Object.keys(data[0]);
    this.tableData = data;
    this._test();//It is called theorically after that the data has been filled
}
_test()
{   
    var link = this.shadowRoot.querySelectorAll(".link");
    //var link = Polymer.dom(this.root).querySelectorAll(".link"); //tried this but i think it is Polymer 1.x style
    //var link = document.querySelectorAll(".link"); // I tried this
    
    console.log(link);
}
<!-- Here is the HTML dom-repeat -->
<tbody>
    <template is="dom-repeat" items="{{tableData}}" as="data">
        <tr>
            <td class="link">{{data.id_Mission}}</td>
            <td>{{data.nom}}</td>
            <td>{{data.resume}}</td>
        </tr>
    </template>
  </tbody>
我错过了什么,

祝大家晚上/晚上/白天愉快

最佳答案

您在 tableData 保存后调用 _test,但元素不会标记到页面中,因为执行线程转到您正在调用的方法,并在可能的时候实际渲染它们。要确认这是否是问题所在,请尝试“发送到后台”您的方法调用,延迟调用它,但“尽快”调用,但使用 setTimeout .

所以你的方法是:

  _buildTable(data)
  {
    this.$.spinner.style.display = 'none';
    this.tableHead = Object.keys(data[0]);
    this.tableData = data;
    setTimeout(() => {
      this._test();
    });
  }

关于javascript - 聚合物 2 查询选择器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825325/

相关文章:

javascript - 使用article_id按钮打开对话框

javascript - D3 "pinning"图表中柱形值的刻度?

angular - 为什么我的 Polymer 2 构建尺寸比 Angular 5 构建尺寸大?

javascript - 在 Polymer 中,我可以使动态创建的纸按钮在单击时改变颜色吗?

javascript - 推送到 dom-repeat 中使用的数据绑定(bind)数组( polymer )

templates - Polymer - DOM 树中元素数据的 dom 重复和缓存

javascript - 如何在 JavaScript 中使用字符串设置对象参数名称?

javascript - 响应式、jQuery 和 .resize

html - polymer 2.0如何让一个元素全面屏?

javascript - 在Polymer 2中进行递归和渲染