Angular2+ render a component without any wrapping tag at all

半腔热情 提交于 2019-12-11 05:42:08

问题


My child-component looks like:

...
@Component({
    selector: '[child-component]'
    templateUrl: './child.template.html'
})
...

and my parent template like:

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

now I want to render the content of my child component without the <span> container around it. I want no container at all in my parent component's template, just the content of our child-component's template.

I tried some other tags already.

  • <ng-content> (nothing rendered at all)
  • <ng-template> (Components on an embedded template:)
  • <ng-container> (Failed to execute 'appendChild' on 'Node')

Thanks in advance!


回答1:


Finally found a working soltion!

My child-component looks like:

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

My child template looks like:

<ng-template #childComponentTemplate>
  ... some content ...
</ng-template>

and my parent template like:

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

This way there is no wrapper at all.



来源:https://stackoverflow.com/questions/52169086/angular2-render-a-component-without-any-wrapping-tag-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!