Is there a way to dynamically create a template for a component based on a value of an injected Service?
Angular 2.0.0-beta.7
The scenario: I get a json containing nested components. I render these components (recursively) using DynamicComponentLoader. Here i inject custom Data to the components.
In this case i have something like a "Layout-Component" which gets a config containing the configuration for the layout (1 row - three column)
The code looks something like that:
{
"components": {
"name": "Layout",
"root": true,
"params": {
"cols": "3"
},
"children": [
{
"name": "Navigation",
"position": "pos1",
"children": [{...}]
},
{
"name": "Content",
"position": "pos2"
},
{
"name": "Sidebar",
"position": "pos3"
}
]
}
}
@Component({
selector: 'df-layout',
template: "<div>?</div>"
})
export class DfLayout {
constructor(@Inject('layoutConfig') layoutConfig) {
//layoutConfig ===> {cols: 3}
let templateString = renderTemplate(layoutConfig);
//TODO
//compile templateString and replace template so that template variables (#pos1, ...) can be used
/*
<div class="row">
<div class="col-4" #pos1></div>
<div class="col-4" #pos2></div>
<div class="col-4" #pos3></div>
</div>
*/
}
}
I know how to create the html based on the config. But I don't know how to "compile" it. Just passing it to [innerHTML]
of the root div doesn't do the job because I need the local template variables for rendering childComponents using DynamicComponentLoader.loadIntoLocation(...)
Is there a way to compile a template-string and use it in the current template?