首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角7- ngTemplateOutlet变量值(表达式)

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

Stack Overflow用户
提问于 2020-03-18 12:12:02
回答 1查看 391关注 0票数 0

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

app.component.ts

代码语言:javascript
复制
export class AppComponent  {
  templateName="SingleSelect";
}

app.component.html

代码语言:javascript
复制
<ng-container *ngTemplateOutlet="{{templateName}}">

</ng-container>

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

{{templateName}}

当然,如果我在下面定义,一切都按预期工作。

代码语言:javascript
复制
<ng-container *ngTemplateOutlet="SingleSelect">

</ng-container>

如何使SingleSelect成为一个可变值?

Stackblitz供参考- https://stackblitz.com/edit/angular-h4mgyq?file=src%2Fapp%2Fapp.component.html

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-18 12:36:43

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

angular.io摘录

ViewChild 配置视图查询的属性装饰器。 支持下列选择器。

  • ..。
  • TemplateRef (例如,带有@ViewChild(TemplateRef)模板的查询;)

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

app.component.ts

代码语言:javascript
复制
import { Component, ViewChild, TemplateRef } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  @ViewChild('SingleSelect', { static: false }) templateComponentVar: TemplateRef<any>;
}

app.component.html

代码语言:javascript
复制
<ng-container *ngTemplateOutlet="templateComponentVar"></ng-container>

<ng-template #SingleSelect>
  <p>Output from test template</p>
</ng-template>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60739330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档