[英]Polymer 1.0 dynamic template with dom-repeat
I have a scenario where I would like to create dynamic template
elements that will be used with Polymer's dom-repeat
. 我有一个场景,我想创建将与Polymer的
dom-repeat
一起使用的动态template
元素。
My current prototype is the following ( JSbin demo ): 我目前的原型是以下( JSbin演示 ):
var domRepeat = document.createElement('template', 'dom-repeat');
domRepeat.items = ['a', 'b', 'c'];
var div = domRepeat.content.ownerDocument.createElement('div');
div.innerHTML = '[[item]]';
domRepeat.content.appendChild(div);
document.body.appendChild(domRepeat);
Polymer.dom.flush();
However , this does not work as intended. 但是 ,这不符合预期。 The output is:
输出是:
[[item]]
[[item]]
[[item]]
rather than: 而不是:
a
b
c
Since the [[item]]
is printed out 3 times, I guess the dom-repeat
itself works, but the data binding does not. 由于
[[item]]
打印出3次,我猜dom-repeat
本身有效,但数据绑定没有。
Plot twist : but if I change the example from dom-repeat
to dom-bind
, then the data binding does work. 情节扭曲 :但如果我将示例从
dom-repeat
更改为dom-bind
,那么数据绑定确实有效。 Changed example, inspired by this answer ( JSBin demo ): 改变了示例,灵感来自这个答案 ( JSBin演示 ):
var domBind = document.createElement('template', 'dom-bind');
domBind.item = 'Hello World!';
var div = domBind.content.ownerDocument.createElement('div');
div.innerHTML = '[[item]]';
domBind.content.appendChild(div);
document.body.appendChild(domBind);
Polymer.dom.flush();
The output is Hello World!
输出是
Hello World!
, as expected. 正如预期的那样。
Any ideas on how to get the first example to work? 关于如何让第一个例子起作用的任何想法?
Binding in dynamically created HTML isn't that easy currently. 动态创建HTML中的绑定目前并不容易。 I think there are plans to support this better eventually.
我认为最终有计划更好地支持这一点。
In the meantime Templatizer should allow to implement such a scenario. 与此同时,Templatizer应该允许实现这样的场景。 I haven't used it myself and haven't found code examples.
我自己没有使用它,也没有找到代码示例。 iron-list and dom-if, dom-bind, dom-repeat seem to make use of it which might work as template for custom implementations.
iron-list和dom-if,dom-bind,dom-repeat似乎使用它可能作为自定义实现的模板。
https://github.com/Polymer/polymer/blob/master/src/lib/template/templatizer.html https://github.com/Polymer/polymer/blob/master/src/lib/template/templatizer.html
This might help http://t-code.pl/blog/2015/08/polymer-templatizer/ 这可能会有所帮助http://t-code.pl/blog/2015/08/polymer-templatizer/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.