angulardart components - dispatch custom event

2019-07-18 03:13发布

问题:

I want to dispatch a custom event from my Angular Dart component to his parent. How can I do this with Angular Dart?

In other words I want do something similar to this: How do you dispatch and listen for custom events in Polymer?

回答1:

Maybe emit does what you want but I assume this works only within Angular. If you want to send DOM events you can do it with dispatchEvent method like

Element e; // assigned by the injector through a constructor argument or aquired by querySelector, ...
...
var event = new CustomEvent(
  type, /* 'myeventname'
  canBubble: canBubble != null ? canBubble : true,
  cancelable: cancelable != null ? cancelable : true,
  detail: {'somekey', 'someValue'}
);
e.dispatchEvent(event);

You can listen for this event by

e.on['myeventname'].listen((e) => print(e.details['somekey']));

or in Polymer (because I saw that you tried to make Angular work together with Polymer)

<some-element on-myeventname="{{myEventHandler}}"></some-element>