Following the example below:
How can I use/create dynamic template to compile dynamic Component with Angular 2.0?
I developed my own template generator, which gets the HTML content directly from a variable.
Here it is:
http://plnkr.co/edit/2Sv1vp?p=preview
Now, my question is... if template content has to interact with the component, for example with a function to execute on click... how can I do that?
Here my app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div>
<h2>An app with DYNAMIC content</h2>
<hr />
<dynamic-detail [tmpl]="tmpl1" [entity]="entity"></dynamic-detail>
<dynamic-detail [tmpl]="tmpl2" [entity]="entity"></dynamic-detail>
</div>`,
})
export class AppComponent {
private tmpl: string;
private entity: any;
constructor() {
this.entity = {
code: "ABC123",
description: "A description of this Entity",
nome: "Bea"
};
this.tmpl1 = '<h2>Sono {{entity.nome}}, il primo template</h2>';
this.tmpl2 = '<a (click)="printSomething()">Sono il secondo template</a>';
}
printSomething() {
console.log("Hello World");
}
}
When I try click on "Sono il secondo template", it should execute printSomething()
function, but instead I obtain this error:
Error in ./CustomDynamicComponent class CustomDynamicComponent - inline template:0:0 caused by: self.context.printSomething is not a function
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…