【问题标题】:Listen to events on ElementList with no explicit accessor在没有显式访问器的情况下侦听 ElementList 上的事件
【发布时间】:2014-12-25 04:11:32
【问题描述】:

是否可以在 ElementList 上侦听没有显式访问器的事件?

例如,我可以对单个元素执行以下操作:

this.querySelector(".js-popover-link").on["on-tap"].listen((event) {
  print("Event Triggered");
});

但是,对于从 querySelectorAll 返回的 ElementList,以下内容是不可能的:

this.querySelectorAll(".js-popover-link").on["on-tap"].listen((event) {
  print("Event Triggered");
});

有没有简单的方法来做到这一点?

【问题讨论】:

    标签: dart dart-html


    【解决方案1】:
    List<StreamSubscription> _subscriptions = <StreamSubscription>[];
    
    this.querySelectorAll(".js-popover-link")
    .forEach((e) {
      _subscriptions.add(e.on["on-tap"].listen((event) {
        print("Event Triggered");
      }));
    });
    

    ...

    _subscriptions.forEach((s) => s.cancel());
    

    【讨论】:

    • 谢谢。有什么方法可以返回单个 StreamSubscription 以便一次调用即可取消侦听器?
    • 没有集成的方法来做到这一点(至少我不知道)。我用一个简单的解决方法更新了我的答案。
    • 谢谢。这也是我能想到的唯一方法。
    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2012-07-01
    相关资源
    最近更新 更多