Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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
Angular 角度2+;渲染一个没有任何包装标记的组件_Angular_Dom_Angular2 Template_Angular2 Directives_Angular2 Ngcontent - Fatal编程技术网

Angular 角度2+;渲染一个没有任何包装标记的组件

Angular 角度2+;渲染一个没有任何包装标记的组件,angular,dom,angular2-template,angular2-directives,angular2-ngcontent,Angular,Dom,Angular2 Template,Angular2 Directives,Angular2 Ngcontent,我的子组件看起来像: ... @Component({ selector: '[child-component]' templateUrl: './child.template.html' }) ... @Component({ selector: 'child-component' templateUrl: './child.template.html' }) export class ChildComponent { @ViewChild('childC

我的子组件看起来像:

...
@Component({
    selector: '[child-component]'
    templateUrl: './child.template.html'
})
...
@Component({
    selector: 'child-component'
    templateUrl: './child.template.html'
})
export class ChildComponent {
  @ViewChild('childComponentTemplate') childComponentTemplate: TemplateRef<any>;
}
<ng-template #childComponentTemplate>
  ... some content ...
</ng-template>
我的父模板如下:

<span child-component [someInput]="123"></span>

现在,我想在不使用
容器的情况下呈现子组件的内容。我不希望父组件的模板中有任何容器,只希望子组件模板的内容

我已经试过其他标签了

  • (完全不渲染)
  • (嵌入模板上的组件:)
  • (未能在“节点”上执行“appendChild”)

提前谢谢

终于找到了一个有效的解决方案

我的子组件看起来像:

...
@Component({
    selector: '[child-component]'
    templateUrl: './child.template.html'
})
...
@Component({
    selector: 'child-component'
    templateUrl: './child.template.html'
})
export class ChildComponent {
  @ViewChild('childComponentTemplate') childComponentTemplate: TemplateRef<any>;
}
<ng-template #childComponentTemplate>
  ... some content ...
</ng-template>
@组件({
选择器:“子组件”
templateUrl:“./child.template.html”
})
导出类子组件{
@ViewChild('childComponentTemplate')childComponentTemplate:TemplateRef;
}
我的子模板类似于:

...
@Component({
    selector: '[child-component]'
    templateUrl: './child.template.html'
})
...
@Component({
    selector: 'child-component'
    templateUrl: './child.template.html'
})
export class ChildComponent {
  @ViewChild('childComponentTemplate') childComponentTemplate: TemplateRef<any>;
}
<ng-template #childComponentTemplate>
  ... some content ...
</ng-template>

... 一些内容。。。
和我的父模板类似:

<child-component #wrapper [someInput]="123"></child-component>
<ng-container *ngTemplateOutlet='wrapper.childComponentTemplate'></ng-container>


这样就根本没有包装了。

您只需使用CSS即可解决此问题,只需将
子组件设置为
显示:内容

子组件{
显示:内容;
}

display:contents
所述,这会使组件看起来像是元素父元素的直接子元素。

我认为这是不可能的:(子组件选择器应该是
Child component
,删除括号并简单渲染
@YashwardhanPauranik,这也将导致DOM中的容器。是的。没有它是不可能的。这是一个很酷的技巧,但不是op想要的。他想从DOM中删除元素,而不仅仅是视觉上的。这个答案吸引了他。)t也许他不需要删除DOM:)使用
display:contents
的元素会忽略自身,这在使用CSS网格或类似布局技术时应该忽略包装器元素时非常有用。