Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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 如何将ng内容放入模板中_Angular - Fatal编程技术网

Angular 如何将ng内容放入模板中

Angular 如何将ng内容放入模板中,angular,Angular,如果我有一个组件BaseComponent <ng-container *ngTemplateOutlet="tpl"></ng-container> 和另一个组件,另一个将模板传递到其中的组件 <base-component [tpl]="tpl">Some content for ng-content</base-component> <ng-template #tpl> <ng-content></n

如果我有一个组件BaseComponent

<ng-container *ngTemplateOutlet="tpl"></ng-container>

和另一个组件,另一个将模板传递到其中的组件

<base-component [tpl]="tpl">Some content for ng-content</base-component>

<ng-template #tpl>
  <ng-content></ng-content>
  <!-- How do I get access to ng-content passed to the base component instead of other component? --> 
</ng-template>
ng内容的某些内容
我是否可以获取传递到模板公开组件中的ng内容

我试图实现的是在应用程序中的其他位置定义一个模板,该模板能够定义组件应该将内容投影到模板中的位置


更多关于我试图实现的内容可以在这个问题中看到,以及我在这个StackBlitz中试图解决这个问题的地方。

您所做的非常有趣,但我认为内容投影无法工作,因为
在组件级别工作,而不是在模板级别

它不“生成”内容,而只是将现有内容投影或移动到组件中

因此,投影内容的生命周期被绑定到声明它的位置,而不是显示它的位置,因此无法使用模板,模板可以在运行时多次实例化

看看这个和这个


然而,我没有从angular公司找到任何官方文件。

我几乎忘了如何让这项工作发挥作用,但前几天晚上我在梦中不知从何而来地找到了答案

在基本组件中,我们可以将ng内容放入模板中,然后将该模板传递给用户定义的模板的上下文

<ng-container *ngTemplateOutlet="tpl;context:{ $implicit: contentTemplate }">
</ng-container>

<ng-template #contentTemplate>
  <ng-content></ng-content>
</ng-template>

然后,当我们定义要传递到基本组件的模板时,我们可以在上下文中使用该模板来显示ng内容

<base-component [tpl]="tpl">Some content for ng-content</base-component>

<ng-template #tpl let-contentTemplate>
  <ng-template *ngTemplateOutlet="contentTemplate"></ng-template>
</ng-template>
ng内容的某些内容
这是一场闪电战


您所说的“访问ng内容”是什么意思?访问什么,基本组件控制器还是基本组件模板?我希望模板中呈现的内容是“ng内容的某些内容”是的,您希望在该点投影内容,然后?问题是什么,我不明白?你是想说你希望基础组件有一个模板投射内容的句柄,这样它就可以投射到自己内部的其他地方??如果我将模板传递到一个组件中,我如何让该模板投射传递到使用它的组件中的内容。ng模板中的内容从创建模板的组件获取内容,而不是使用模板的内容。放弃这个方向几个月后,我终于找到了实现方法。