angular - Angular2+ 渲染一个完全没有任何包装标签的组件
问题描述
我的子组件看起来像:
...
@Component({
selector: '[child-component]'
templateUrl: './child.template.html'
})
...
和我的父模板,如:
<span child-component [someInput]="123"></span>
现在我想在没有<span>
容器的情况下渲染我的子组件的内容。我的父组件模板中根本不需要容器,只需要子组件模板的内容。
我已经尝试了其他一些标签。
<ng-content>
(根本没有渲染)<ng-template>
(嵌入式模板上的组件:)<ng-container>
(无法在 'Node' 上执行 'appendChild')
提前致谢!
解决方案
终于找到了一个可行的解决方案!
我的子组件看起来像:
@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>
这种方式根本没有包装器。
推荐阅读
- jenkins - 在同一个 Jenkinsfile 中定义多个作业
- snowflake-cloud-data-platform - Snowflake Copy 命令不会在定义的表中插入数据
- python - Django 评论/优化/建议
- swift - 在 Swift 中使用 CMMotionManager 的问题
- python - 列中两个坐标到矩阵的距离
- javascript - 无法复制反应表 CodeSandbox:本地主机上的 API 错误
- c++ - NgHttp2 调用请求数据处理程序两次,针对一个请求
- javascript - 如何更新 Recoil.js 外部组件中的原子(状态)?(反应)
- kubernetes - k8s什么时候重试liveness probe?
- python - 有没有一种基于另一个数据框中的数据创建数据框列的有效方法?