Nate
Nate

Reputation: 7856

Access exported component within the component

I would like to send the component from within the component meaning when I have this:

@Component({
    selector: 'my-component',
    templateUrl: 'my.template.html'
})
export class MyComponent {
    private myVar: any;

    constructor(){

    }
    myFunction() =>{

    }
}

Outside of the class I can simply call:

import { MyComponent } from './my.component';

But I would like to get MyComponent within the class:

 myFunction() =>{
     this.myVar = **MyComponment**
 }

How to get **MyComponent**?

Please note that I don't want the instance but need the class itself.

EDIT: I realize my question is not clear enough. Here is what I need to do... I need to extract the component and recreate it somewhere else. In another component I have the following:

export class MyOtherComponent {    
    constructor(private viewContainer: ViewContainerRef, private componentFactoryResolver: ComponentFactoryResolver){

    }
    createComponent(myComponent) =>{
        var factory = this.componentFactoryResolver.resolveComponentFactory(myComponent);
        var compRef = this.viewContainer.createComponent(factory);
        container.getElement().append($(compRef.location.nativeElement));
    }
}

So Inside MyComponent I want to be able to do this:

myFunction() =>{
    this.myOtherComponent.createComponent(**MyComponent**)
}

Upvotes: 0

Views: 49

Answers (1)

toskv
toskv

Reputation: 31600

You should be able to just refer to it with the class name.

class MyComponent {
    private myVar: MyComponent;

    constructor(){
        console.log("created!")
    }
    myFunction () {
        var myVar = MyComponent;
        new myVar();
    }
}

You can check out the resulting js here.

Upvotes: 1

Related Questions