0
内部方法遵循下面的例子:角2:功能/上点击的动态模板(动态组件)
How can I use/create dynamic template to compile dynamic Component with Angular 2.0?
我开发我自己的模板生成器,它直接从变量获取HTML内容。 这就是: http://plnkr.co/edit/2Sv1vp?p=preview
现在,我的问题是...如果模板内容必须与组件进行交互,例如用函数来执行上点击......我该怎么办呢?
这里我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");
}
}
当我尝试点击 “索诺IL SECONDO模板”,应该执行printSomething()
功能,而是我得到这个错误:
Error in ./CustomDynamicComponent class CustomDynamicComponent - inline template:0:0 caused by: self.context.printSomething is not a function
哦,非常感谢你!这对我有用;) –