search

Home  >  Q&A  >  body text

angular.js - Ideas for dynamically creating subcomponents in angular2

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.

迷茫迷茫2800 days ago909

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-15 17:11:19

    It’s ready, use the template command

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-15 17:11:19

    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></

    reply
    0
  • Cancelreply