简体   繁体   English

ngFor 中的动态模板引用变量(Angular 9)

[英]Dynamic template reference variable inside ngFor (Angular 9)

How to declare a dynamic template reference variable inside a ngFor element?如何在ngFor元素内声明动态模板引用变量

I want to use the popover component from ng-bootstrap, the popover code (with Html binding) is as shown:我想使用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>

How can I wrap those elements inside ngFor ?如何这些元素包装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>

Hmmm... any idea?嗯……有什么想法吗?

Template reference variables are scoped to the template they are defined in. A structural directive creates a nested template and, therefore, introduces a separate scope.模板引用变量的作用域限定于它们在其中定义的模板。结构指令创建一个嵌套模板,因此引入了一个单独的作用域。

So you can just use one variable for your template reference所以你可以只使用一个变量作为你的模板参考

<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>

and it should work because it has already declared inside <ng-template ngFor它应该可以工作,因为它已经在<ng-template ngFor

Plunker ExamplePlunker 示例

For more details see also:有关更多详细信息,另请参阅:

This is the best solution I've found: https://stackoverflow.com/a/40165639/3870815这是我找到的最佳解决方案: https : //stackoverflow.com/a/40165639/3870815

In that answer they use:在那个答案中,他们使用:

@ViewChildren('popContent') components:QueryList<CustomComponent>;

To build a list of those dynamically generated components.构建那些动态生成的组件的列表。 Highly recommend you check it out!强烈建议您检查一下!

Another way to allow this is to create a component that wraps the button and the ng-template另一种允许这样做的方法是创建一个包装按钮和 ng-template 的组件

<div *ngFor="let member of members">
    <popover-button [member]="member"></pop-over-button>
</div>

And have the following in the popover-button component并在 popover-button 组件中包含以下内容

<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>

You can use trackBy: trackByFn in *ngFor你可以在*ngFor使用trackBy: trackByFn *ngFor

<div *ngFor="let member of members;trackBy: trackByF">
    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM