简体   繁体   English

如何使用ngFor在angular2中加载多个子组件?

[英]How to load multiple child components in angular2 using ngFor?

I have created few child components with given names using dynamic Component loader in angular2. 我使用angular2中的动态组件加载器创建了几个具有给定名称的子组件。 & I am able to load them using their selectors in static way. &我能够使用其选择器以静态方式加载它们。

for eg. 例如 I can load my component using 我可以使用以下方式加载组件

<DYNAMIC-BODY-1>Loading</DYNAMIC-BODY-1>

But my problem is that these component names are stored in json file. 但是我的问题是这些组件名称存储在json文件中。 So I want to read that JSON File & load that component using its variable name. 因此,我想读取该JSON文件并使用其变量名加载该组件。

for eg. 例如 if uiComponent="DYNAMIC-BODY-1"; 如果uiComponent="DYNAMIC-BODY-1"; then i should be able to load my component as <{{uiComponent}}>Loading</{{uiComponent}}> 那么我应该能够将我的组件加载为<{{uiComponent}}>Loading</{{uiComponent}}>

here is my html: 这是我的HTML:

<div class="row">
    <div *ngFor="#comp of record">        
        <div *ngFor="#pRow of comp.pageObj.pageRows">
            <div *ngFor="#sec of pRow.sections">
                <div *ngFor="#sRow of sec.sectionRows" class="col-md-{{(12/3)}}">
                    <div *ngFor="#srColumn of sRow.secRowColumns">
                       //prints component name i.e. DYNAMIC-BODY-1
                       {{srColumn.uiComponent}} 
                       //Loads the component
                       <DYNAMIC-BODY-1>Loading</DYNAMIC-BODY-1>
                       //what I want to achieve,this does not work
                       <div [innerHTML]="'<' + srColumn.uiComponent+ '></' + srColumn.uiComponent + '>'"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

I tried multiple ways ,but I am not able to get the result. 我尝试了多种方法,但无法获得结果。 Is there any way to do that? 有什么办法吗?

 import {Component,DynamicComponentLoader,Injector} from 'angular2/core'; import {NgSwitch, NgSwitchWhen, NgSwitchDefault,NgFor} from 'angular2/common'; import {Http, Response} from 'angular2/http'; import {Observable} from 'rxjs/Rx'; import { DataService } from '../../app/services/data.service'; import {DynamicBody1Component} from './../DYNAMIC-BODY-1/DYNAMIC-BODY-1.component' import {DynamicBody2Component} from './../DYNAMIC-BODY-2/DYNAMIC-BODY-2.component' import {FooterComponent} from './../FOOTER/FOOTER.component' import {HeadingComponent} from './../heading/heading.component' import {ImageComponent} from './../IMAGE/IMAGE.component' import {StaticBodyComponent1} from './../STATIC-BODY-1/STATIC-BODY-1.component' import {StaticBodyComponent2} from './../STATIC-BODY-2/STATIC-BODY-2.component' @Component({ selector: 'Parser', providers: [DataService], templateUrl: 'app/Parser/Parser.component.html', directives: [DynamicBody1Component, DynamicBody2Component, FooterComponent,HeadingComponent,ImageComponent,StaticBodyComponent1,StaticBodyComponent2] }) export class ParserComponent { public record; public componentName; public absolutePath; constructor(dcl: DynamicComponentLoader, injector: Injector,private dataService: DataService) { } ngOnInit() { this.componentName="Parser"; this.absolutePath="app/"+this.componentName+"/"+this.componentName+"-MODEL.json"; this.dataService.getData(this.absolutePath) .subscribe((data:any[]) => { this.record = data; console.log(this.componentName); }); } subscribe({ complete: () => {setTimeOut(() => { dcl.loadAsRoot(DynamicBody1Component, '#dynamic-body-1', injector); dcl.loadAsRoot(DynamicBody2Component, '#dynamic-body-2', injector); dcl.loadAsRoot(FooterComponent, '#footer', injector); dcl.loadAsRoot(HeadingComponent, 'heading', injector); dcl.loadAsRoot(ImageComponent, '#image', injector); dcl.loadAsRoot(StaticBodyComponent1, '#static-body-1', injector); dcl.loadAsRoot(StaticBodyComponent2, '#static-body-2', injector); }, 1)}); } 

You can't load components this way 您不能以这种方式加载组件

<{{uiComponent}}>Loading</{{uiComponent}}>

You can't even create dynamic HTML this way. 您甚至无法以这种方式创建动态HTML。 To create HTML you could 要创建HTML,您可以

<div [innerHTML]="'<' + uiComponent + '></' + uiComponent + '>'"></div>

To dynamically add components you can use DynamicComponentLoader 要动态添加组件,可以使用DynamicComponentLoader

You also can't have that dcl.loadAsRoot(...) code in the constructor. 您也不能在构造函数中使用dcl.loadAsRoot(...)代码。 At this time the dynamically create HTML doesn't exist yet. 目前,动态创建的HTML尚不存在。
You can try to move that code to the 您可以尝试将代码移至

subscribe({/* process data - code omitted*/, 
    complete: () => {setTimeOut(() => {
    /* here the DOM should be rendered and dcl... should work */
    }, 1)});

to wait until the data arrived and than a bit more to give Angular time to render the DOM. 等到数据到达,再给Angular时间渲染DOM。

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

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