我遇到了一个我无法解决的问题。我需要从代码中添加<iron-list>
元素,分配其项目,依此类推。
我已经用谷歌搜索了几天,但是在所有示例中,我发现<iron-list>
已添加到HTML中。
我已经试过了:
var msgs = new List();//I've added some data to this list
var listTab = new Element.div();
var list = new Element.tag("iron-list");
var template = new Element.tag("template");
var item = new Element.tag("paper-item");
item.innerHtml = "[[item.text]]";
template.children.add(item);
list.children.add(template);
list.items = msgs;
list.as = "item";
listTab.children.add(list);
这会导致以下错误:
iron-list requires a template to be provided in light-dom
Uncaught Unhandled exception:
TypeError: this.ctor is not a function
以下代码导致相同的错误:
var msgs = new List();//I've added some data to this list
var listTab = new Element.div();
IronList list = new IronList();
var template = new Element.tag("template");
var item = new Element.tag("paper-item");
item.innerHtml = "[[item.text]]";
template.children.add(item);
list.children.add(template);
list.items = msgs;
list.as = "item";
listTab.children.add(list);
最后,我尝试了一个非常简单的解决方案,该解决方案也不起作用:
var list= new Element.html('<iron-list items="{{msgs}}" as="item"><template><paper-item>[[item.text]]</paper-item></template></iron-list>');
listTab.children.add(list);
错误:
Polymer::Attributes: couldn`t decode Array as JSON
Removing disallowed element <IRON-LIST> from [object DocumentFragment]
Removing disallowed element <PAPER-ITEM> from [object DocumentFragment]
Removing disallowed element <ARRAY-SELECTOR> from iron-list
Uncaught Unhandled exception:
Bad state: No element
我真的很感谢从 Dart 代码添加
<iron-list>
元素的工作解决方案。
最佳答案
var validator = new NodeValidatorBuilder.common()
..allowElement('iron-list', attributes: ['items', 'as'])
..allowElement(...);
var list= new Element.html('<iron-list items="{{msgs}}" as="item"><template><paper-item>[[item.text]]</paper-item></template></iron-list>',
validator: validator);
listTab.children.add(list);
要么
var list= new Element.html('<iron-list items="{{msgs}}" as="item"><template><paper-item>[[item.text]]</paper-item></template></iron-list>',
treeSanitizer: NodeTreeSanitizer.trusted);
listTab.children.add(list);
关于polymer - 动态添加铁 list 及其所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466747/