简体   繁体   English

将数据从动态加载的组件传递回父页面Angular2

[英]Pass data from dynamicallyloadedcomponent back to parent page Angular2

I'm wondering if there is any way to pass data back from a dynamically loaded component in Angular2. 我想知道是否有任何方法可以将数据从Angular2中的动态加载组件传回。 I pass the component data using 我使用传递组件数据

this._dcl.loadIntoLocation(...).then(component => {
  component.instance.variable = this.variable;
}

On completing the tasks inside the component, in my case a modal, can I pass data or variables back out in a similar manner? 在完成组件内部的任务(以我的方式为模态)后,是否可以类似的方式传递数据或变量? Thanks in advance. 提前致谢。

You can add an Observable to the dynamically added component 您可以将Observable添加到动态添加的组件中
(an EventEmitter might work as well but might break eventually. Then Angular2 team doesn't guarantee that an EventEmitter will keep behaving like an observable) (一个EventEmitter可能也可以正常工作,但最终可能会崩溃。然后Angular2团队不保证EventEmitter会一直保持可观察的状态)

Then just subscribe to this observable 然后只订阅这个可观察的

this._dcl.loadIntoLocation(...).then(component => {
  component.instance.variable = this.variable;
  component.instance.someObservable.subscribe(val => this.someVal = val);
}

DynamicComponentLoader is deprecated and AFAIK was already removed in master. 不建议使用DynamicComponentLoader ,并且已经在母版中删除了AFAIK。

See also Angular 2 dynamic tabs with user-click chosen components 另请参阅带有用户单击所选组件的Angular 2动态选项卡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM