Data binding for dynamically generated HTML in Polymer?

陌路散爱 提交于 2019-12-10 09:42:37

问题


When I write the following inside my <template>-tag, everything works fine:

<ul id="breadcrumbList" class="breadcrumb">
    <li><a on-click="{{breadcrumbClick}}">{{overviewName}}</a></li>
</ul>

I dynamically generated a new <li>-element of the same structure, like this:

crumb = document.createElement("li");
crumb.innerHTML = '<a on-click="{{breadcrumbClick}}">'+category+'</a>';

But when I click this element, the event-handler isn't called.

The event-handler looks like this:

breadcrumbClick: function(event, detail, sender) {
     /*reaction*/
}

I did not find any documentation about whether it's possible or impossible to use data binding for dynamically generated content.


回答1:


This is possible with injectBoundHTML(). We haven't documented it yet, but you can see the method signature and demo here: https://github.com/Polymer/docs/issues/607

Example:

<li id="myli></li>

this.injectBoundHTML('<a on-click="{{breadcrumbClick}}">...</a>', this.$.myli);


来源:https://stackoverflow.com/questions/25783778/data-binding-for-dynamically-generated-html-in-polymer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!