Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular 4 ng内容选择不处理动态创建的转包元素_Javascript_Angular_Transclusion - Fatal编程技术网

Javascript Angular 4 ng内容选择不处理动态创建的转包元素

Javascript Angular 4 ng内容选择不处理动态创建的转包元素,javascript,angular,transclusion,Javascript,Angular,Transclusion,我正在使用ComponentFactoryResolver动态创建要插入到模板中的元素,该模板使用ng content进行转换 在我将select属性添加到我的ng内容之前,这一切都非常有效。请看哪一个演示了这个问题。如果我从app.ts第63行的HeaderComponent模板中删除content top属性,模板将按预期呈现 但是,我确实需要使用select,因为要注入两个不同的模板片段,所以我不能简单地删除它 感谢您的帮助。角度转换仅适用于直系儿童。一种方法是使用ngTemplateOu

我正在使用
ComponentFactoryResolver
动态创建要插入到模板中的元素,该模板使用
ng content
进行转换

在我将
select
属性添加到我的
ng内容
之前,这一切都非常有效。请看哪一个演示了这个问题。如果我从
app.ts
第63行的
HeaderComponent
模板中删除
content top
属性,模板将按预期呈现

但是,我确实需要使用
select
,因为要注入两个不同的模板片段,所以我不能简单地删除它


感谢您的帮助。

角度转换仅适用于直系儿童。一种方法是使用
ngTemplateOutlet
从动态组件中提升内容:

<some-other-component>
    <ng-template content-host></ng-template>
    <ng-template top-content [ngTemplateOutlet]="topContent"></ng-template>
    <ng-template bottom-content [ngTemplateOutlet]="bottomContent"></ng-template>
</some-other-component>

组件。ts

topContent: TemplateRef<any>;
bottomContent: TemplateRef<any>

const componentRef = viewContainerRef.createComponent(componentFactory);
const instance = componentRef.instance;
componentRef.changeDetectorRef.detectChanges();

this.topContent = instance.templates.find(x => x.place === 'top-content').templateRef;
this.bottomContent = instance.templates.find(x => x.place === 'bottom-content').templateRef;

topContent:TemplateRef

如果我删除第63行的
contenttop
属性,我看不出有什么区别。如果我从
ng content
中删除
select
,我会看到
HeaderComponent
已呈现角度仅包含直接子项。请尝试
select=“[content host]”
对不起,你说得很对,我的意思是从第18行删除
select
。我需要能够选择在
ng模板中被替换的元素,并且
仍然被替换。在本例中,
将是顶层。
因为要注入两个不同的模板片段,所以我不能简单地删除它。
您能扩展您的示例吗?哪些片段也将被注入?
@ViewChildren(TranscludeMeToDirective) templates: QueryList<TranscludeMeToDirective>;