简体   繁体   English

在Dart中创建聚合物元素的实例,它不是HtmlElement

[英]Create an Instance of Polymer Element in Dart, which is not a HtmlElement

I have a PolymerElement called NewEntryElement, and I want it to be created and added if a Button is clicked. 我有一个名为NewEntryElement的PolymerElement,如果要单击一个Button,我希望可以创建和添加它。

new-Entry.html new-Entry.html

<polymer-element name="new-Entry" attributes="metaData">
  <!-- a table here -->
</polymer-element>

new-Entry.dart new-Entry.dart

@CustomTag('new-entry')
class NewEntryElement extends PolymerElement {
  @published List<Map> metaData;

  factory NewEntryElement() => document.createElement("new-entry");

  NewEntryElement.created() : super.created() {
    polymerCreated();
  }
}

Calling the factory in my button function does not work, since a HtmlElement is created and it can't be cast to NewEntryElement. 在我的按钮函数中调用工厂不起作用,因为创建了HtmlElement,并且无法将其强制转换为NewEntryElement。 So i also tried to call this in my Button function: 所以我也试图在我的Button函数中调用它:

var newEntry = new Element.tag('new-entry');
(newEntry as NewEntryElement).metaData = this.metaData;

There is the same Problem... So I need a way to get an Instance of my PolymerElement so i can assign the attribute metaData to the Element. 存在相同的问题...所以我需要一种方法来获取PolymerElement的实例,以便可以将metaData属性分配给Element。

Btw: this is the Exception: 顺便说一句:这是例外:

Exception: Uncaught Error: type 'HtmlElement' is not a subtype of type 'NewEntryElement' in type cast.

This normally works without problems. 这通常可以正常工作。 There seems to be something wrong with your polymer initialization code or element imports. 您的聚合物初始化代码或元素导入似乎有问题。

You have named your element "new-Entry" in HTML but use "new-entry" in code (upper-/lower-case e ) this is probably what causes the problem. 您已在HTML中将元素命名为"new-Entry" ,但在代码中使用了"new-entry" (大写/小写e ),这可能是导致问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM