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.
巴扎黑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>
belowimport {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></