angulardart components - dispatch custom event

2019-07-18 03:41发布

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条回答
狗以群分
2楼-- · 2019-07-18 03:58

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>
查看更多
登录 后发表回答