I want to dynamically create a 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 the following 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 document (Angular 2 Synchronous Dynamic Component Creation)
And understand that I can use either
- Kind of dynamic
ngIf
withComponentFactoryResolver
. If I pass known components 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, the 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 a solution with the Compiler
.