Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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 角度7-ngTemplateOutlet变量值(表达式)_Angular_Ng Container - Fatal编程技术网

Angular 角度7-ngTemplateOutlet变量值(表达式)

Angular 角度7-ngTemplateOutlet变量值(表达式),angular,ng-container,Angular,Ng Container,我无法通过变量传递ng容器中*ngTemplateOutlet的值 app.component.ts export class AppComponent { templateName="SingleSelect"; } app.component.html <ng-container *ngTemplateOutlet="{{templateName}}"> </ng-container> <ng-template #SingleSelect> &

我无法通过变量传递ng容器中*ngTemplateOutlet的值

app.component.ts

export class AppComponent  {
  templateName="SingleSelect";
}
app.component.html

<ng-container *ngTemplateOutlet="{{templateName}}">

</ng-container>

<ng-template #SingleSelect>
<p>Output from test template</p>
</ng-template>

{{templateName}}

测试模板的输出

{{templateName}}
当然,如果我在下面定义,一切都会按预期进行

<ng-container *ngTemplateOutlet="SingleSelect">

</ng-container>

如何让SingleSelect选择变量值

Stackblitz供参考-

对于这样的用例,您首先必须通过查询捕获定义的模板,幸运的是,该查询也支持

摘自

视图儿童 配置视图查询的属性装饰器

支持以下选择器

  • TemplateRef(例如,使用@ViewChild(TemplateRef)模板进行查询;)

请注意ng模板“SingleSelect”是如何捕获到下面的
templateComponentVar
中的:

app.component.ts

export class AppComponent  {
  templateName="SingleSelect";
}
从'@angular/core'导入{Component,ViewChild,TemplateRef};
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
@ViewChild('SingleSelect',{static:false})templateComponentVar:TemplateRef;
}
app.component.html

<ng-container *ngTemplateOutlet="{{templateName}}">

</ng-container>

<ng-template #SingleSelect>
<p>Output from test template</p>
</ng-template>

{{templateName}}

测试模板的输出


谢谢这正是我需要的