动态组件中的输出参数

Avi*_*Avi 4 angular

当我们希望Parent组件侦听子事件时,我们使用@output参数并订阅父标记:

<my-tag (onMyEvent)="onMyEvent($event)"></my-tag>
Run Code Online (Sandbox Code Playgroud)

我如何使用ComponentResolver做到这一点?

Gün*_*uer 10

这不受支持.
- 使用带有动态添加的组件的共享服务ViewContainerRef.createComponent()
- 使用componentRef它返回到必须连接的输入和输出.

this.resolver.resolveComponent(this.type).then((factory:ComponentFactory<any>) => {
  this.cmpRef = this.target.createComponent(factory);
  this.cmpRef.instance.someOutput.subscribe(...)
  this.cmpRef.instance.someInput = this.someInputValue;
});
Run Code Online (Sandbox Code Playgroud)

  • 再次感谢.我使用了这个解决方案:家长和孩子通过服务进行沟通https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service (2认同)