问题
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