I tried to create a component dynamic like that:
Here is my file dynamicComponent.ts:
import {Directive, Input, ViewContainerRef, Component, ReflectiveInjector, ComponentFactory, ComponentFactoryResolver} from '@angular/core';
export function createComponentFactory(resolver: ComponentFactoryResolver, metadata: Component): Promise<ComponentFactory<any>> {
const cmpClass = class DynamicComponent {
};
const decoratedCmp = Component(metadata)(cmpClass);
return new Promise((resolve) => {
resolve(resolver.resolveComponentFactory(decoratedCmp));
});
}
@Directive({
selector: 'dynamic-html-outlet'
})
export class DynamicComponent {
@Input() src: string;
constructor(public vcRef: ViewContainerRef, public resolver: ComponentFactoryResolver) {
}
ngOnChanges() {
if (!this.src) return;
const metadata = new Component({
selector: 'dynamic-component',
template: this.src
});
createComponentFactory(this.resolver, metadata)
.then(factory => {
const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
this.vcRef.createComponent(factory, 0, injector, []).changeDetectorRef.detectChanges();
});
}
}
Here is the app.module.ts :
@NgModule({
declarations: [
MyApp,
DynamicComponent,
...APP_PAGES,
...APP_DIRECTIVES
],
imports: [
IonicModule.forRoot(MyApp),
NgReduxModule,
MomentModule,
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: (http: Http) => new TranslateStaticLoader(http, '../assets/i18n', '.json'),
deps: [Http]
})
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
//DynamicComponent,
//ScrollableHeader,
AppLink,
...APP_PAGES,
...APP_DIRECTIVES
],
providers: [
...APP_SERVICES
]
})
The component AppLink that have to be loaded is defined Something like that:
@Component({
selector: 'app-link',
template: '<a (click)="openLink($event);"><ng-content></ng-content></a>',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppLink {
@Input() link;
//some method here
}
I got always this error:
polyfills.js:3 Unhandled Promise rejection: Template parse errors: Can't bind to 'link' since it isn't a known property of 'app-link'.
Thanks for any helps?