3

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.

5
  • Do you have any documentation of this being a actual possibility? Because I needed the same feature one year ago and it ended up to be impossible back then due to JIT being ditched for AOT which doesn't allow dynamic templates to be made after the initial build. Commented Jul 7, 2018 at 8:02
  • @uday214125 As your code says , template is being built , is it rendering ? Commented Jul 7, 2018 at 8:09
  • @ShaikNizamuddin yes it is rendering as i am using property binding as well. See my updated query. Commented Jul 7, 2018 at 8:20
  • refer stackoverflow.com/questions/37487977/… and stackoverflow.com/questions/46815473/… Commented Jul 7, 2018 at 10:25
  • You can create a service to share data between you components Commented Jul 7, 2018 at 11:20

0

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.