[英]Angular4 Template
I have written a simple code to demonstrate ngTemplate 我编写了一个简单的代码来演示ngTemplate
<div> <ng-container [ngTemplateOutlet] ="template1"> </ng-container></div>
and here are the template 这是模板
<ng-template #template1> This is 1st template </ng-template>
<ng-template #template2>This is 2nd template </ng-template>
It is working as expected, but my problem is I am not able to pass dynamic value to [ngTemplateOutlet]
它按预期工作,但我的问题是我无法将动态值传递给
[ngTemplateOutlet]
I want to achieve something like this 我希望实现这样的目标
<div> <ng-container [ngTemplateOutlet] ="selectTemplate"></ng-container> </div>
Which will either select template1
or template2
. 哪个将选择
template1
或template2
。
Whenever I am passing some dynamic value which I have defined in my typescript file it gives is error 每当我传递一些我在我的打字稿文件中定义的动态值时,它给出的是错误
ERROR TypeError: templateRef.createEmbeddedView is not a function
My typescript file contains this code 我的打字稿文件包含此代码
if(some condition) {
this.selectTemplate = "template1";
} else {
this.selectTemplate = "template2";
}
In your first example, you're binding ngTemplateOutlet
to the value of the expression template1
, which is the first ngTemplate
element. 在第一个示例中,您将
ngTemplateOutlet
绑定到表达式template1
的值,该表达式是第一个ngTemplate
元素。 So far so good. 到现在为止还挺好。
In your second example, you're binding ngTemplateOutlet
to the value of the expression selectTemplate
, which is not a template element - you've set it to be a string! 在第二个示例中,您将
ngTemplateOutlet
绑定到表达式selectTemplate
的值,该表达式不是模板元素 - 您已将其设置为字符串!
To fix this, you first need to add the template references to your component as fields using the ViewChild
annotation: 要解决此问题,首先需要使用
ViewChild
批注将模板引用添加到组件中作为字段:
@ViewChild('template1') template1;
@ViewChild('template2') template2;
Then set selectTemplate
to refer to the element itself rather than its name: 然后设置
selectTemplate
以引用元素本身而不是其名称:
if (some condition) {
this.selectTemplate = this.template1;
}
else {
this.selectTemplate = this.template2;
}
Note that the ViewChild
variables will not be set until the component's view has been initialized - you can use the ngViewAfterInit
hook to wait for that to happen. 请注意,在组件的视图初始化之前,不会设置
ViewChild
变量 - 您可以使用ngViewAfterInit
挂钩等待它发生。
update Angular 5 更新Angular 5
ngOutletContext
was renamed to ngTemplateOutletContext
ngOutletContext
已重命名为ngTemplateOutletContext
See also https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29 另见https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29
original 原版的
You can pass a context 您可以传递上下文
<ng-container [ngTemplateOutlet]="template1; context: {foo: 'foo', bar: 'bar', $implicit: 'implit' }"
In the template you can use it like 在模板中,您可以使用它
<ng-template #template1 let-foo1="foo" let-foo2="bar" let-item>
This is 1st template
<div>foo1: {{foo1}}</div>
<div>foo2: {{foo2}}</div>
<div>foo1: {{item}}</div>
</ng-template>
I hope the names I used are not too confusing. 我希望我使用的名字不会太混乱。 I tried to make the distinction between
我试图区分
="somename"
in the template variable definintion ( let-item
) let-item
)中不需要="somename"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.