Listen to events on ElementList with no explicit accessor

北战南征 提交于 2020-01-15 12:24:10

问题


Is it possible to listen to events that have no explicit accessor on an ElementList?

For example I can do the following on a single element:

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

However, the following is not possible on the ElementList returned from querySelectorAll:

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

Is there a simple way to do this?


回答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());


来源:https://stackoverflow.com/questions/26630644/listen-to-events-on-elementlist-with-no-explicit-accessor

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