0

I am using the following:

this.DynamicComponentLoader.loadIntoLocation(NewComponent, this.parent, 'new');

where this.parent is an ElementRef of my main class App.

However, this throws me an error of:

TypeError: Cannot read property 'componentView' of undefined.

I create this.parent like that:

setParent(parent){
    this.parent = parent.nativeElement;
}

and my App class:

export class App{
    constructor(_elementRef:ElementRef){
        Service.setParent(_elementRef);
    }
}

And my NewComponent:

import {Component} from "angular2/core";

@Component({
    selector: 'new',
    templateUrl: 'app/New.html'
})

export class NewComponent{
    constructor(){
        console.log("New Comopnent here")
    }
}

and Service:

export class Service{
    private parent:ElementRef;
    constructor(private DynamicComponentLoader:DynamicComponentLoader){

    }

    setParent(parent){
        this.parent = parent.nativeElement;
    }

    append(){
        this.DynamicComponentLoader.loadIntoLocation(ModalComponent, this.parent, 'modal');
    }
}

Any ideas on where is the problem?

10
  • stackoverflow.com/questions/36325212/… contains a working Plunker example. Commented Apr 8, 2016 at 13:02
  • Your code doesn't show where you use this.DynamicComponentLoader.loadIntoLocation(NewComponent, t... This doesn't work in the constructor for example. Commented Apr 8, 2016 at 13:03
  • @GünterZöchbauer, I've updated my question. I use it in function Commented Apr 8, 2016 at 13:05
  • is it injectable service you have just mentioned? Commented Apr 8, 2016 at 13:07
  • And I should mentiont, that class App is main class, that is bootstraped. Commented Apr 8, 2016 at 13:07

1 Answer 1

1

loadIntoLocation expects at least these parameter/types:

loadIntoLocation(type: Type, hostLocation: ElementRef, anchorName: string)

You use the nativeElement of parent, this is not an ElementRef but an HTMLElement.

The solution is, when storing the parent, you should use:

 setParent(parent){
   this.parent = parent;
 }

I can only guess that this is where your problem comes from

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.