【发布时间】:2017-11-10 11:21:31
【问题描述】:
如何在ngFor 元素中声明动态 模板引用变量?
我想使用ng-bootstrap的popover组件,popover代码(带有Html绑定)如图:
<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 中?
<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>
嗯……有什么想法吗?
【问题讨论】:
-
没有动态引用变量这样的东西。为什么你认为它需要是动态的?
-
因为他们的教程说为了在弹出框内有 html 绑定,那么我们需要创建一个
ng-template并使用 模板引用变量 引用它,但现在我想要在ngFor元素中使用此弹出框 -
照样做。即使名称相同,每个元素的模板变量也会有所不同。
-
如果您对所有内容都使用相同的 ref 会发生什么?你测试过吗?
-
哈,我从来没想过……我现在就测试一下……因为我一直在思考如何用“index”声明一个** 模板引用变量 "**... 待我测试后会更新... :D
标签: angular ng-bootstrap angular-template