Angular 4 Dynamic component loading & emitting data to parent component

折月煮酒 提交于 2019-12-08 03:20:22

问题


I am on process of creating poc for dynamic form generation using custom components.

i have succeeded upto some extent, where i can create form using set of dynamic components according to a json array. Now i am stuck in one instance where i just need to get output or rather the data out of these components ones the user have update the relevant inputs.

I had a look at the @output decorator and the eventEmitter yet i couldn't get a proper solution out of it. Much appreciate if any one in the community could give me some headsup :)

please follow the PLUNKER to view the current progress and my approach.


回答1:


You are on the right track, There are two approaches you can use here

(i) Service

(ii) Using eventEmitter - output decorator

Since the components are dynamic you can always subscribe and the service can send the data back to app.ts.

Inorder to get an idea have a look at this Passing Input while creating Angular 2 Component dynamically using ComponentResolver

In your example, I modified service addDynamic component method and added Output events to each component, that will belongs to app.ts

DEMO



来源:https://stackoverflow.com/questions/46815473/angular-4-dynamic-component-loading-emitting-data-to-parent-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!