去除飞镖中的聚合物元素

removing polymer elements in dart

我正在添加聚合物元素。我想在单击其(自己的)图像时删除元素(自身)。根据封装,我将不得不使 parent 删除 child。但这也需要为 parent 生成聚合物元素(我在这里吗??)。 children.add 没有为聚合物定义 elements.how 我要这样做吗,我计划在将聚合物元素添加到表单之前做一些检查。

已读: How to remove a child component with a delete button in the child itself

How do I fire a custom event from Polymer Dart?

How do you dispatch and listen for custom events in Polymer?

我通过调度事件传递什么?

init-item.html

<polymer-element name="init-item" >
  <template>
    <input type="image" src="button_minus_red.gif" on-click="{{remove}}" width = "15" height ="15">
    {{Name}}<br> 
  </template>
  <script type="application/dart" src="init-item.dart"></script>
</polymer-element>

init-item.飞镖

@HtmlImport('init-item.html')
import 'dart:html';
import 'dart:async';
import 'package:polymer/polymer.dart';
@CustomTag('init-item')
class PItem extends PolymerElement{
  @observable String Name='hello';
  void remove(){
    Name='';

  }

所以目前在点击图片时名称属性被重置。但这只发生在从 main.dart 生成的后续点击事件上。这是由于数据绑定。我如何启动双向数据绑定。??

main.html(聚合物标签不顾建议添加。无法弄清楚)

...
<form  id="youritems">
</form>
...

聚合物项目添加到上面的标签。

main.dart

...
main() async{
.. initPolymer().then((_) {});
}
..
...{..
var input = new InputElement(type:"image");
input.onClick.listen((p){
            p.preventDefault();
            populateYourPlayers(teams[i]);
          });
}
void populateItems(String name){
  Polymer.onReady.then((_) { 
    var pl = new Element.tag('player-item');
    pl.playerName = name;
    yourPlayer.children.add(pl);
    });
}
..

如果您将点击处理程序从 remove 重命名为例如 removeItem,那么您只需在处理程序中调用 remove() 即可。

<input type="image" src="button_minus_red.gif" on-click="{{removeItem}}" width = "15" height ="15">
void removeItem(){
  Name='';
  remove(); // removes the init-item (self)
}

remove 是所有元素上的现有方法。如果您将自己的方法命名为 remove,您将覆盖默认的 remove 方法,并且无法再调用原始方法(除了在您的元素中使用 super.remove();