I know a lot of similar questions have been asked, and i've been searching for quite a while now to try every possible solution, and it still doesn't work. Most of the solutions involve adding entryComponent
or adding it to the entryComponent
of the component itself (bad practise) but both didn't help.
So i've been trying to implant the same technique as here: plunkr.
My code:
app.module.ts
@NgModule({
declarations: [
AppComponent,
****
ArtworkWrapperComponent,
TestComponent
],
imports: [
***
],
entryComponents: [TestComponent],
providers: [Animations, PageNameService],
bootstrap: [AppComponent]
})
export class AppModule { }
So TestComponent
is what i'm trying to display based on Input value, this is handled by a class identical to DclWrapper from the Plunkr example.
I'm using Angular CLI for this project (might be relevant):
angular-cli: 1.0.0-beta.24
node: 6.2.1
os: darwin x64
@angular/common: 2.4.1
@angular/compiler: 2.4.1
@angular/core: 2.4.1
@angular/forms: 2.4.1
@angular/http: 2.4.1
@angular/platform-browser: 2.4.1
@angular/platform-browser-dynamic: 2.4.1
@angular/router: 3.4.1
@angular/compiler-cli: 2.4.1
Update
Ok so i figured out what i did wrong, i misread the example Plunkr where it already puts the components in an array, is it possible to resolve the component based on a string? like this: Plunkr
Got it working like this, but would like to know if there's a neater solution:
var artwork = null;
switch(this.type) {
case 'Test1Component':
artwork = Test1Component;
break;
case 'Test2Component':
artwork = Test2Component;
break;
}
if(artwork) {
let factory = this.componentFactoryResolver.resolveComponentFactory(artwork);
this.cmpRef = this.target.createComponent(factory)
}