Angular2+ 渲染组件时根本不需要任何包装标签

2024-01-09

我的子组件如下所示:

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

我的父模板如下:

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

现在我想渲染我的子组件的内容而不需要<span>周围有容器。我不想在父组件的模板中出现任何容器,只需要子组件模板的内容。

我已经尝试过其他一些标签了。

  • <ng-content>(根本没有渲染任何东西)
  • <ng-template>(嵌入模板上的组件:)
  • <ng-container>(无法在“节点”上执行“appendChild”)

提前致谢!


终于找到了可行的解决方案!

My 子组件好像:

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

My 子模板好像:

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

and my 父模板 like:

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

这样就根本没有包装器了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular2+ 渲染组件时根本不需要任何包装标签 的相关文章