Radim Köhler Radim Köhler - 3 months ago 469
TypeScript Question

How can I use/create dynamic template to compile Component with Angular2 (RC6++)?

I want to dynamically create template. This should be used to build a

ComponentType
at Runtime and place (even replace) it somewhere inside of the hosting Component.

Until RC4 I was using
ComponentResolver
, but with RC5 I get message:


ComponentResolver
is deprecated for dynamic compilation. Use
ComponentFactoryResolver
together with
@NgModule/@Component.entryComponents
or ANALYZE_FOR_ENTRY_COMPONENTS provider instead. For runtime compile only, you can also use
Compiler.compileComponentSync/Async
.


I found this (offical angular2) document

Angular 2 Synchronous Dynamic Component Creation



And understand that I can use either


  • Kind of dynamic
    ngIf
    with
    ComponentFactoryResolver
    . If I will pass known components into hosting one inside of
    @Component({entryComponents: [comp1, comp2], ...})
    - I can use
    .resolveComponentFactory(componentToRender);

  • Real runtime compilation, with
    Compiler
    ...



But the question is how to use that
Compiler
? The Note above says that I should call:
Compiler.compileComponentSync/Async
- so how?

For example. I want to create (based on some configuration conditions) this kind of template for one kind of settings

<form>
<string-editor
[propertyName]="'code'"
[entity]="entity"
></string-editor>
<string-editor
[propertyName]="'description'"
[entity]="entity"
></string-editor>
...


and in another case this one (
string-editor
is replaced with
text-editor
)


<form>
<text-editor
[propertyName]="'code'"
[entity]="entity"
></text-editor>
...


And so on (different number/date/reference
editors
by property types, skipped some properties for some users...)
. I.e. this is an example, real configuration could generate much more different and complex templates.

The template is changing, so I cannot use
ComponentFactoryResolver
and pass existing ones... I need solution with the
Compiler

Answer

EDIT - related to RC6++

Similar topic is discussed here Equivalent of $compile in Angular 2. We need to use RuntimeCompiler and NgModule. Read more about NgModule in Angular2 here:

TL&DR

There is a working plunker/example (dynamic template, dynamic component type, dynamic module,RuntimeCompiler, ... in action)

The principal is:
1) Create Template
2) Create Component
3) Create Module
4) Compile Module
5) use Target to create an Instance of it

Here is a code snippet (more of it here)

  var componentType = ... // build a component
  var runtimeModule = ... // build a module with component

  // compile module
  this.compiler
    .compileModuleAndAllComponentsAsync(runtimeModule)
    .then((moduleWithFactories) =>
    {
        // lo-dash to find THE factory
        let factory = _.find(moduleWithFactories.componentFactories
                            , { componentType: componentType });
        // Target will instantiate and inject component (we'll keep reference to it)
        this.componentRef = this
            .dynamicComponentTarget
            .createComponent(factory);

        // let's inject @Inputs to component instance
        let component = this.componentRef.instance;

        component.entity = this.entity;
        ...
    });

This is it - in nutshell it. To get more details.. read below

.

Detailed explanation - Angular2 RC6++ & runtime components

Below description of this scenario, we will

  1. create a module PartsModule:NgModule (holder of small pieces)
  2. create another module DynamicModule:NgModule, which will contain our dynamic component (and reference PartsModule dynamically)
  3. create dynamic Template (simple approach)
  4. create new Component type (only if template has changed)
  5. create new RuntimeModule:NgModule. This module will contain the previously created Component type
  6. call RuntimeCompiler.compileModuleAndAllComponentsAsync(runtimeModule)
  7. Use just built ComponentFactory and assign @Inputs to new instance (switch from INPUT to TEXTAREA editing)

NgModule

We need an NgModules.

While I would like to show a very simple example, in this case, I would need three modules (in fact 4 - but I do not count the AppModule). Please, take this rather than a simple snippet as a basis for a really solid dynamic component generator.

There will be one module for all small components, e.g. string-editor, text-editor (date-editor, number-editor...)

@NgModule({
  imports:      [ 
      CommonModule,
      FormsModule
  ],
  declarations: [
      DYNAMIC_DIRECTIVES
  ],
  exports: [
      DYNAMIC_DIRECTIVES,
      CommonModule,
      FormsModule
  ]
})
export class PartsModule { }

Where DYNAMIC_DIRECTIVES are extensible and are intended to hold all small parts used for our dynamic Component template/type. Check app/parts/parts.module.ts

The second will be module for our Dynamic stuff handling. It will contain hosting components and some providers.. which will be singletons. Therefor we will publish them standard way - with forRoot()

import { DynamicDetail }          from './detail.view';
import { DynamicTypeBuilder }     from './type.builder';
import { DynamicTemplateBuilder } from './template.builder';

@NgModule({
  imports:      [ PartsModule ],
  declarations: [ DynamicDetail ],
  exports:      [ DynamicDetail],
})

export class DynamicModule {

    static forRoot()
    {
        return {
            ngModule: DynamicModule,
            providers: [ // singletons accross the whole app
              DynamicTemplateBuilder,
              DynamicTypeBuilder
            ], 
        };
    }
}

Check the usage of the forRoot() in the AppModule

Finally, we will need an adhoc, runtime module.. but that will be created later, as a part of DynamicTypeBuilder job.

Read (do read) much more about NgModule there:

A template builder

In our example we will process detail of this kind of entity

entity = { 
    code: "ABC123",
    description: "A description of this Entity" 
};

To create a template, in this plunker we use this simple/naive builder.

The real solution, a real template builder, is the place where your application can do a lot

// plunker - app/dynamic/template.builder.ts
import {Injectable} from "@angular/core";

@Injectable()
export class DynamicTemplateBuilder {

    public prepareTemplate(entity: any, useTextarea: boolean){

      let properties = Object.keys(entity);
      let template = "<form >";
      let editorName = useTextarea 
        ? "text-editor"
        : "string-editor";

      properties.forEach((propertyName) =>{
        template += `
          <${editorName}
              [propertyName]="'${propertyName}'"
              [entity]="entity"
          ></${editorName}>`;
      });

      return template + "</form>";
    }
}

A trick here is - it builds a template which uses some set of known properties, e.g. entity. Such property(-ies) must be part of dynamic component, which we will create next.

To make it a bit more easier, we can use an interface to define properties, which our Template builder can use. This will be implemented by our dynamic Component type.

export interface IHaveDynamicData { 
    public entity: any;
    ...
}

A Component builder and Module

Very important thing here is to keep in mind:

our type, build with our DynamicTypeBuilder, could differ - but only in its template (build above). Components properties (inputs, outputs or some protected) are still same. If we need different properties, we should define different combination of Template and Type Builder

// plunker - app/dynamic/type.builder.ts
import {Component, NgModule, Input, Injectable} from '@angular/core';    
import { PartsModule }   from '../parts/parts.module';    
export interface IHaveDynamicData { 
    public entity: any;
}

@Injectable()
export class DynamicTypeBuilder {

  // this object is singleton - so we can use this as a cache
  private _cacheOfTypes  : { [templateKey: string]: any } = {};
  private _cacheOfModules: { [templateKey: string]: any } = {};

  public createComponentAndModule(template: string): {type: any, module: any} {
    let module;
    let type = this._cacheOfTypes[template];

    if (type) {
       module = this._cacheOfModules[template];
       console.log("Module and Type are returned from cache")
       return { type: type, module: module };
    }

    // unknown template ... let's create a Type for it
    type   = this.createNewComponent(template);
    module = this.createComponentModule(type);

    // cache that type and module - because the only difference would be "template"
    this._cacheOfTypes[template]   = type;
    this._cacheOfModules[template] = module;

    return { type: type, module: module };
  }

Above we create and cache both Component and Module. Because if the template (in fact the real dynamic part of that all) is the same.. we can reuse

And here are two methods, which represent the really cool way how to create a decorated classes/types in runtime. Not only @Component but also the @NgModule

protected createNewComponent (tmpl:string) {
  @Component({
      selector: 'dynamic-component',
      template: tmpl,
  })
  class CustomDynamicComponent  implements IHaveDynamicData {
      @Input()  public entity: any;
  };
  // a component for this particular template
  return CustomDynamicComponent;
}
protected createComponentModule (componentType: any) {
  @NgModule({
    imports: [
      PartsModule, // there are 'text-editor', 'string-editor'...
    ],
    declarations: [
      componentType
    ],
  })
  class RuntimeComponentModule
  {
  }
  // a module for just this Type
  return RuntimeComponentModule;
}

Important:

our component dynamic types differ, but just by template. So we use that fact to cache them. This is really very important. Angular2 will also cache these.. by the type. And if we would recreate for the same template strings new types... we will start to generate memory leaks.

RuntimeCompiler used by hosting component

Final piece is a component, which hosts the target for our dynamic component, e.g. <div #dynamicContentPlaceHolder></div>. We get a reference to it and use RuntimeCompiler to provide us with a Factory to create a component. That is in a nutshell, and here are all the pieces of that component (if needed, open plunker here)

Let's firstly summarize import statements:

import {Component, ComponentRef,ViewChild,ViewContainerRef}   from '@angular/core';
import {AfterViewInit,OnInit,OnDestroy,OnChanges,SimpleChange} from '@angular/core';
import {RuntimeCompiler}                                      from "@angular/compiler";

import { IHaveDynamicData, DynamicTypeBuilder } from './type.builder';
import { DynamicTemplateBuilder }               from './template.builder';

@Component({
  selector: 'dynamic-detail',
  template: `
<div>
  check/uncheck to use INPUT vs TEXTAREA:
  <input type="checkbox" #val (click)="refreshContent(val.checked)" /><hr />
  <div #dynamicContentPlaceHolder></div>  <hr />
  entity: <pre>{{entity | json}}</pre>
</div>
`,
})
export class DynamicDetail implements AfterViewInit, OnChanges, OnDestroy, OnInit
{ 
    // wee need Dynamic component builder
    constructor(
        protected typeBuilder: DynamicTypeBuilder,
        protected templateBuilder: DynamicTemplateBuilder,
        protected compiler: RuntimeCompiler,
    ) {}
    ...

We just receive, template and component builders (our code) and the Angular2 RuntimeCompiler.

Properties which are needed for our example (more in comments)

// reference for a <div> with #dynamicContentPlaceHolder
@ViewChild('dynamicContentPlaceHolder', {read: ViewContainerRef}) 
protected dynamicComponentTarget: ViewContainerRef;
// this will be reference to dynamic content - to be able to destroy it
protected componentRef: ComponentRef<IHaveDynamicData>;

// until ngAfterViewInit, we cannot start (firstly) to process dynamic stuff
protected wasViewInitialized = false;

// example entity ... to be recieved from other app parts
// this is kind of candiate for @Input
protected entity = { 
    code: "ABC123",
    description: "A description of this Entity" 
  };

In this simple scenario, our hosting component does not have any @Input. So it does not have to react to changes. But despite of that fact (and to be ready for coming changes) - we need to introduce some flag if the component was already (firstly) initiated. And only then we can start the magic.

Also, we need to keep a reference to compiled template.. to be able properly destroy() it, whenever we will change it.

// this is the best moment where to start to process dynamic stuff
public ngAfterViewInit(): void
{
    this.wasViewInitialized = true;
    this.refreshContent();
}
// wasViewInitialized is an IMPORTANT switch 
// when this component would have its own changing @Input()
// - then we have to wait till view is intialized - first OnChange is too soon
public ngOnChanges(changes: {[key: string]: SimpleChange}): void
{
    if (this.wasViewInitialized) {
        return;
    }
    this.refreshContent();
}

public ngOnDestroy(){
  if (this.componentRef) {
      this.componentRef.destroy();
      this.componentRef = null;
  }
}

And finally RuntimeCompiler. It gets template, Component type from above providers. It also gets the NgModule - runtime one - and calls compileModuleAndAllComponentsAsync(runtimeModule). That will result in a built module with factories. We will use one, which belongs to CustomType... and path it to the Target placeholder to instatiate the Component

protected refreshContent(useTextarea: boolean = false){

  if (this.componentRef) {
      this.componentRef.destroy();
  }

  // here we get a TEMPLATE with dynamic content === TODO
  var template = this.templateBuilder.prepareTemplate(this.entity, useTextarea);

  // here we get a Component with its Module 
  var result = this.typeBuilder.createComponentAndModule(template);

  var componentType = result.type;
  var runtimeModule = result.module

  // compile module
  this.compiler
    .compileModuleAndAllComponentsAsync(runtimeModule)
    .then((moduleWithFactories) =>
    {
        // lo-dash to find THE factory
        let factory = _.find(moduleWithFactories.componentFactories
                            , { componentType: componentType });
        // Target will instantiate and inject component (we'll keep reference to it)
        this.componentRef = this
            .dynamicComponentTarget
            .createComponent(factory);

        // let's inject @Inputs to component instance
        let component = this.componentRef.instance;

        component.entity = this.entity;
        //...
    });
}

And that is pretty it. Do not remember to Destroy anything what was build dynamically (ngOnDestroy). Also, do cache dynamic types, modules if they differ - just by template.

Check it all in action here

to see previous versions (e.g. RC5 related) of this post, check the history