polymer - 动态添加铁 list 及其所有数据

标签 polymer dart

我遇到了一个我无法解决的问题。我需要从代码中添加<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/

相关文章:

firebase - 在Firebase上保存时的引号

forms - 可以使用 ngControl 绑定(bind) Polymer paper-dropdown-menu 吗?

image - flutter : The argument type 'Image' can't be assigned to the parameter type 'IconData'

firebase - Flutter Web - 未创建 Firebase 应用程序 '[DEFAULT]' - 调用 Firebase App.initializeApp() (app/no-app)

flutter - 定义列表和 map 的不同方法( Dart/flutter 打)

string - flutter/Dart : Convert HEX color string to Color?

javascript - Firefox 上的 Polymer.js 组件

javascript - 从 Iron-List 内的元素读取数据属性

javascript - Polymer iron-ajax 绑定(bind)值到重复模板中的纸项

Dartium 显示空白页