Basically, I need to create component based on dynamic template and share parent data to newly created component and also share data to other components.
// import
@Component({
selector: 'app-modal',
template: `<div #vcf></div>`,
providers: [AppsService],
encapsulation: ViewEncapsulation.None,
})
export class AppModalComponent implements OnInit, OnDestroy, AfterViewInit {
@ViewChild('vcf', {read: ViewContainerRef}) vcf: ViewContainerRef;
private cmpRef: ComponentRef<any>;
constructor(
private appService: AppsService,
private compiler: Compiler,
private injector: Injector,
private m: NgModuleRef<any>
) {}
ngOnInit() { }
ngAfterViewInit() {
let template;
const styles = [`input { width: 100px }`];
this.appService.getAppTemplate().subscribe((templateData) => {
console.log('dynamic html data ::: ', templateData); // ok
template = templateData;
// Component dynamically.
const templateComponent = Component({template})(class DynamicTemplateComponent {
@Input() sendSMS; // Error, Saying Decorators are not valid here
someMethod() {
alert('some data submitting'); // working
}
addSMSData() {
alert('ok');
}
});
const templateModule = NgModule({
imports: [RouterModule, FormsModule, CommonModule],
declarations: [templateComponent]
})(class {});
this.compiler.compileModuleAndAllComponentsAsync(templateModule)
.then((factories) => {
const f = factories.componentFactories[0];
this.cmpRef = f.create(this.injector, [], null, this.m);
this.cmpRef.instance.name = 'templateFrm';
this.vcf.insert(this.cmpRef.hostView);
});
});
}
}
Here my dummy template which is being rendering and working also.
<input type="text" id="sms_title" [(ngModel)] ="sms_title" name="sms_title" placeholder="SMS title" required> <button type="submit" (click)="addSMSData()">Submit</button>
So how to use decorators inside the newly created component which has everything on the fly.
@Input() getSMSData; // Error, Decorators are not valid here
Some idea will be appreciable.
Thanks.