angular.js - Ideas for dynamically creating subcomponents in angular2
迷茫
迷茫 2017-05-15 17:09:19
0
2
894

The overall page is a component template. There is a child component that has been declared and used in the parent component template. Now I want to dynamically create the child component inside the child component. I have tried this:

getViewRef is used to dynamically create sub-components FileUploadEle
Although it can be created correctly, the created element does not belong to PhoneComponent
as shown in the figure:

Becomes a brother element.
What to do to correctly create parent-child instead of sibling.

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
滿天的星座

It’s ready, use the template command

巴扎黑

I don’t understand your specific needs, but I think it’s better to use template declaration instead of creating components in js.

Pay attention to <ng-content>

below
import {Component} from '@angular/core';

@Component({
    selector: 'parent',
    template: `
        <p>
            <h1>parent</h1>
            <ng-content></ng-content>
        </p>
    `
})
export class ParentComponent {}

@Component({
    selector: 'child',
    template: `<h1>child</h1>`
})
export class ChildComponent {}

@Component({
    selector: 'demo',
    template: `<parent><child></child></
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template