我正在尝试在运行时创建一个 ParagraphElement 并将其添加到聚合物组件中的 Div 元素,如下所示:
<polymer-element name="my-element" extends="div">
<template>
<p>
<button on-click="updateMessage" data-msg="You clicked Show">Show Message</button>
<button on-click="updateMessage" data-msg="You clicked Hide">Hide Message</button>
<button on-click="addPelement" >Add P element</button>
</p>
<p id="message">{{ message }}</p> <!-- bind the message -->
<div id="mydiv"></div>
</template>
<script type="application/dart" src="my_element.dart"></script>
</polymer-element>
和飞镖文件如下:
@CustomTag('my-element')
class MyElement extends PolymerElement with ObservableMixin {
@observable var message;
void updateMessage(Event e, var detail, Element target) {
message = target.attributes['data-msg']; // extract the data- attribute
}
void addPelement(Event e) {
var item = new ParagraphElement ();
item.text="new p element";
DivElement d = query("#mydiv");
d.children.add(item);
}
}
行 query("#mydiv") 返回一个空元素。