Programmatically create special Polymer-Element

泄露秘密 提交于 2020-02-02 07:14:05

问题


I have got a polymer-element with following html:

<polymer-element name="tab-bar">
  <template>
    <button>Hello</button>
    <template repeat="{{item in items}}">
      <div>This element is {{ item }}</div>
    </template>
  </template>
  <script type="application/dart" src="tab_bar.dart"></script>
</polymer-element>

The underlying dart class looks as follows:

import 'package:polymer/polymer.dart';

@CustomTag('tab-bar')
class TabBar extends PolymerElement {
  List items;

  TabBar(List<String> items) {
    this.items = toObservable(items);
  }
}

With the following approach, it isn't possible to programmatically add the element:

query('body').children.add(createElement(new TabBar(['One','Two','Three'])));

So now, how can I add such a polymer element programatically and also set the list in the constructor?


回答1:


As of polymer 0.8.5 you can use constructor like

new Element.tag('tag-bar');

also, no more .xtag and no more .host (host == this now)

credits go to Seth Ladd who explained this on polymer mailing list




回答2:


Note: this only works for polymer.dart < 0.8.5. See other answer for the new way.

Custom elements don't really have constructors like we're familiar with them in Dart. Instead, try this:

var tabBar = createElement('tag-bar');
tabBar.xtag.items = toObservable(['a','b','c']);

query('body').children.add(tabBar);


来源:https://stackoverflow.com/questions/18961141/programmatically-create-special-polymer-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!