首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ngFor内的动态模板参考变量(角9)

ngFor内的动态模板参考变量(角9)
EN

Stack Overflow用户
提问于 2017-06-08 16:17:53
回答 4查看 97.1K关注 0票数 139

如何在元素中声明动态模板引用变量?

我想使用ng引导程序中的popover组件,弹出代码(带有Html绑定)如下所示:

代码语言:javascript
复制
<ng-template #popContent>Hello, <b>{{name}}</b>!</ng-template>
<button type="button" class="btn btn-secondary" [ngbPopover]="popContent" popoverTitle="Fancy content">
    I've got markup and bindings in my popover!
</button>

我如何包装在ngFor

代码语言:javascript
复制
<div *ngFor="let member of members">
    <!-- how to declare the '????' -->
    <ng-template #????>Hello, <b>{{member.name}}</b>!</ng-template>
        <button type="button" class="btn btn-secondary" [ngbPopover]="????" popoverTitle="Fancy content">
        I've got markup and bindings in my popover!
    </button>
</div>

嗯..。知道吗?

EN

回答 4

Stack Overflow用户

发布于 2017-06-08 16:31:49

模板引用变量的作用域是定义在其中的模板。结构指令创建嵌套模板,因此引入一个单独的作用域.

因此,您只需使用一个变量作为模板引用。

代码语言:javascript
复制
<div *ngFor="let member of members">
  <ng-template #popupContent>Hello, <b>{{member.name}}</b>!</ng-template>
  <button type="button" class="btn btn-secondary" [ngbPopover]="popupContent" popoverTitle="Fancy content">
      I've got markup and bindings in my popover!
  </button>
</div>

而且它应该可以工作,因为它已经在<ng-template ngFor中声明了

柱塞实例

有关更多细节,请参见:

票数 149
EN

Stack Overflow用户

发布于 2019-01-24 16:56:19

这是我找到的最好的解决方案:https://stackoverflow.com/a/40165639/3870815

在这个答复中,他们使用:

代码语言:javascript
复制
@ViewChildren('popContent') components:QueryList<CustomComponent>;

若要生成那些动态生成的组件的列表,请执行以下操作。强烈推荐你去看看!

票数 37
EN

Stack Overflow用户

发布于 2020-07-27 11:28:27

另一种允许这样做的方法是创建一个封装按钮和ng模板的组件。

代码语言:javascript
复制
<div *ngFor="let member of members">
    <popover-button [member]="member"></pop-over-button>
</div>

并在popover按钮组件中包含以下内容

代码语言:javascript
复制
<ng-template #popContent>Hello, <b>{{member.name}}</b>!</ng-template>
    <button type="button" class="btn btn-secondary" [ngbPopover]="popContent" popoverTitle="Fancy content">
    I've got markup and bindings in my popover!
</button>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44440879

复制
相关文章

相似问题

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