Create new Child Component within a Child Component

Kamui

I have an issue about creating new Components with the resolveComponentFactory. First there is a startComponent (parent component) and from this component there are several buttons and every btn creates an new child Component. For example now I create a "childComponent" and this also works. But now I want to create an new childComponent within the childComponent and this new component shall have the startComponent as parent component, not the childComponent itself. So I Need a way to call the addComponent() method from the startComponent with my childComponent.

Here is the way I'm doing it at the moment:

startComponent.ts:

import {
Component, OnInit, ViewChild,
ComponentFactoryResolver,
ViewContainerRef
} from '@angular/core';

Import { childComponent } from '../childComponent/child.component';
import { DataService } from '../data.service';

@Component({
selector: 'app-start',
templateUrl: './start.component.html',
styleUrls: ['./start.component.css']
})


export class startComponent implements OnInit {
@ViewChild('parent', { read: ViewContainerRef }) container: ViewContainerRef;

constructor(private dataService: DataService, private componentFactoryResolver: ComponentFactoryResolver){}

ngOnInit(){}

addComponent(){

let componentFactory = this.componentFactoryResolver.resolveComponentFactory(childComponent);

let component = this.container.createComponent(componentFactory);
// next Line i save the reference of the "childComponent" to a Service
// so the "childComponent" can get it and destroy himself if wanted
this.createComponentService.setReference(component, type);
}
}

startComponent.html

<div>
<span matTooltip="Select"><button (click)="addComponent()" class="btn">
<img class="img" src="./assets/StartIcons/childCreate-icon.png" alt="not found"> </button></span>
</div>

<div #parent></div>

It does work if I want to create a childComponent with my childComponent, but the startComponent is not the parent component then. I hope you understand my Problem, else I can try to explain it again.

Yerkon

But now I want to create an new childComponent within the childComponent and this new component shall have the startComponent as parent component, not the childComponent itself. So I Need a way to call the addComponent() method from the startComponent with my childComponent.

So, in childComponent you should have reference to parentComponent(StartComponent). You can get it by injecting to new added childComponent:

childComponent:

constructor(private parentComp: StartComponent){

}

As you have reference to it, you get access to properties, methods of parent and within childComponent can call addComponent() easily like:

parentComp.addComponent();

Update

Interesting, dynamically created component doesn't have parent component in injector. So, it can't inject parent StartComponent.

Another solution

  1. Set child component's parent property with StartComponet:

    ngOnInit() {
       this.comps.clear();
       let aComponentFactory = 
       this.componentFactoryResolver.resolveComponentFactory(this.compArr[0]);
       let aComponentRef = this.comps.createComponent(aComponentFactory);
    
       (<AComponent>aComponentRef.instance).name = 'A name';
       (<AComponent>aComponentRef.instance).parent = this;
     }
    

    StackBlitz Demo. Look at the console

  2. Manually inject parent component in child:

    constructor(public injector: Injector ) {
        console.log('child injector', injector);
        this.parent = injector.get(AppComponent);
    }
    
    ngOnInit() {
       console.log('parent is here', this.parent);
       this.parent.test();
    }
    

    StackBlitz Demo. Look at the console

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating a child component within a parent component in Vue.JS

From Dev

Creating a child component within a parent component in Vue.JS

From Dev

Sending new data do child component in Angular

From Dev

Child Component Isn't Updating with New Props

From Dev

How to create an instance of service in component and provide it for child?

From Dev

Create child components in the parent component in Angular

From Dev

Create child Component after Observable data is available

From Dev

Close child component from the child component itself

From Dev

Get state of a child component

From Dev

Styling not applying to child component

From Java

Call a method of the child component

From Dev

Execute method on child component

From Dev

Get component child as string

From Dev

Child component is not rendering props

From Dev

Vuex not working in child component

From Dev

Child Component Not Binding in React

From Dev

Mount parent component in child

From Dev

Passing method to child Component

From Dev

Router link in child component

From Dev

Child component not called

From Dev

Child component not updating in React

From Java

Passing to child component an event handlers within slot in vue 3

From Dev

Binding new types in a component so that child components can inject them

From Dev

Angular 2 new Router: How to get router parameters of a child component?

From Dev

Binding new types in a component so that child components can inject them

From Dev

How do I use data from Vue.js child component within parent component?

From Dev

How to loop thorough object and create child component in React.js

From Dev

Getting reference to child component in parent component

From Dev

React child component causing a reaction in parent component

Related Related

  1. 1

    Creating a child component within a parent component in Vue.JS

  2. 2

    Creating a child component within a parent component in Vue.JS

  3. 3

    Sending new data do child component in Angular

  4. 4

    Child Component Isn't Updating with New Props

  5. 5

    How to create an instance of service in component and provide it for child?

  6. 6

    Create child components in the parent component in Angular

  7. 7

    Create child Component after Observable data is available

  8. 8

    Close child component from the child component itself

  9. 9

    Get state of a child component

  10. 10

    Styling not applying to child component

  11. 11

    Call a method of the child component

  12. 12

    Execute method on child component

  13. 13

    Get component child as string

  14. 14

    Child component is not rendering props

  15. 15

    Vuex not working in child component

  16. 16

    Child Component Not Binding in React

  17. 17

    Mount parent component in child

  18. 18

    Passing method to child Component

  19. 19

    Router link in child component

  20. 20

    Child component not called

  21. 21

    Child component not updating in React

  22. 22

    Passing to child component an event handlers within slot in vue 3

  23. 23

    Binding new types in a component so that child components can inject them

  24. 24

    Angular 2 new Router: How to get router parameters of a child component?

  25. 25

    Binding new types in a component so that child components can inject them

  26. 26

    How do I use data from Vue.js child component within parent component?

  27. 27

    How to loop thorough object and create child component in React.js

  28. 28

    Getting reference to child component in parent component

  29. 29

    React child component causing a reaction in parent component

HotTag

Archive