【发布时间】:2017-03-12 14:09:53
【问题描述】:
我试图在视图中动态选择模板,只需使用 Angular 2 的传统“#”字符引用它们。 在我的项目中,我处理错误并将它们显示给用户,我有一个对话框组件,它的内容应该基于 html 动态注入,这就是我使用模板的原因。
我阅读了一些文章,其中展示了当我已经知道模板引用的名称时如何做到这一点的方法,在我的情况下,我不知道引用的名称,我在运行时获得了名称。 我特别遵循了本指南:https://www.bennadel.com/blog/3101-experimenting-with-dynamic-template-rendering-in-angular-2-rc-1.htm
所以目前我的对话框组件有以下视图:
<template #err_1 let-property1="p1" let-property2="p2">
property1: {{p1}}
property2: {{p2}}
</template>
<template #err_2 let-property1="p1" let-property2="p2">
<p *ngIf="p1">{{p1}}</p>
property2: {{p2}}
</template>
<!--The code for the template directive i took from the guide in the link above-->
<tem [render]="templateRef"
[context]="context">
</tem>
在我的 dialog.ts 中,我有以下代码:
@Component({
selector: 'error-dialog',
queries: {
templateRef: new ViewChild("err_1")
},
templateUrl: './dialog.html'
})
...
“TemplateRendererDirective”指令源是我从上面链接的指南中获取的。 请注意让我感到困惑的地方:即使指令最终获得 TemplateRef 实例,templateRef 基本上也会获得一个对象:“ViewChild”,这怎么可能?
所以只有当我知道我要渲染哪个错误模板时,例如:“err_1”我只是在 dialog.ts 中预先引用它,但事实并非如此,我想动态告诉我要渲染“err_1”,“ err_2" 等。并给出上下文(这是用数据填充该模板的对象 - 例如 p1、p2,也是动态的)
有可能吗?
【问题讨论】:
-
你到底想要什么?更改
templateRef参数? -
动态设置 templateRef 参数,而不是像我在帖子中显示的那样声明对话框组件时。但是如何从 ViewChild 对象创建 TemplateRef?我需要 templateRef 对象,因为将内容注入视图的函数 createEmbededView 期望获得 templateRef
-
ViewChild如果您的err_1哈希与<template>相关,则自动抓取TemplateRef -
它与模板元素有关,那么如何从 ViewChild 获取 templateRef 对象?当我尝试简单地转换打字稿编译器时会出现错误
-
添加请编码它的外观
标签: javascript angular