简体   繁体   English

如何使用 angular 在“ngFor”循环内创建模板引用?

[英]How to create template reference inside the `ngFor` loop with angular?

i am trying to create template reference through ngFor loop.我正在尝试通过ngFor循环创建模板引用。 but not works.但不起作用。

ts file: ts文件:

import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  members = [
    {
      name: 'name1',
      id:"one"
    },
    {
      name: 'name2',
      id:"two"
    },
    {
      name: 'name3',
      id:"three"
    } 
  ]
}

template:模板:

<div class="container">
  <h2>Test popover</h2>
  <p>
    How to declare a dynamic template reference variable inside a ngFor element?
  </p>
</div>

<div *ngFor="let member of members">
  <ng-container [ngTemplateOutlet]="{{member.id}}"
    >Hello, <b>{{ member.name }}</b
    >!</ng-container
  >
</div>

<ng-template #one> one </ng-template>
<ng-template #two> two </ng-template>
<ng-template #three> three </ng-template>

Live demo 现场演示

I am getting an error as:我收到一个错误:

Type '{ "": any; }' is not assignable to type 'TemplateRef<any>'. Object literal may only specify known properties, and '""' does not exist in type 'TemplateRef<any>'.

Update:更新:

If you want to pass ngTemplate to child, you can use contentChild如果你想将ngTemplate传递给孩子,你可以使用contentChild

import { Component, Input, ContentChild, TemplateRef } from '@angular/core';

@Component({
  selector: 'hello',
  template: `
  <div *ngFor="let member of members">
      {{member.name}} <ng-container [ngTemplateOutlet]="this[member.id]"></ng-container>
  </div>
  `,
  styles: [`h1 { font-family: Lato; }`],
})
export class HelloComponent {
  @ContentChild('one') one: TemplateRef<any>;
  @ContentChild('two') two: TemplateRef<any>;
  @ContentChild('three') three: TemplateRef<any>;
  @Input() members: any;
}

forked stackblitz分叉的堆栈闪电战

Solution1:解决方案1:

Instead of hardcoding the template names, you can just render based on the index您可以根据索引进行渲染,而不是对模板名称进行硬编码

<div class="container">
  <h2>Test popover</h2>
  <p>
    How to declare a dynamic template reference variable inside a ngFor element?
  </p>
</div>
<div *ngFor="let member of members; let i = index">
  <ng-container *ngIf="temps">
    <ng-container [ngTemplateOutlet]="temps.toArray()[i]"
      >Hello, <b>{{ member.name }}</b
      >!</ng-container
    ></ng-container
  >
</div>

<ng-template #temp> one </ng-template>
<ng-template #temp> two </ng-template>
<ng-template #temp> three </ng-template>

forked stackblitz分叉的堆栈闪电战

Solution2:解决方案2:

You can chain ternary conditions so that you get the output as required您可以链接三元条件,以便根据需要获得 output

<div class="container">
  <h2>Test popover</h2>
  <p>
    How to declare a dynamic template reference variable inside a ngFor element?
  </p>
</div>

<div *ngFor="let member of members">
  <ng-container
    *ngTemplateOutlet="
      member.id === 'one' ? one : member.id === 'two' ? two : three
    "
    >Hello, <b>{{ member.name }}</b
    >!</ng-container
  >
</div>

<ng-template #one> one </ng-template>
<ng-template #two> two </ng-template>
<ng-template #three> three </ng-template>

forked stackblitz分叉的堆栈闪电战

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

相关问题 如何在* ngFor中设置唯一的模板引用变量? (角度) - How to set unique template reference variables inside an *ngFor? (Angular) 如何为* ngFor中的项目创建不同的模板参考变量 - How to create different template reference variable for items in *ngFor angular ngFor 中的动态模板引用变量(Angular 9) - Dynamic template reference variable inside ngFor (Angular 9) Angular - 如何在 NgFor 中创建动态模板变量 - Angular - How to create dynamic template variable inside NgFor 在Angular 6中使用ngFor循环动态创建输入元素数组并基于模板引用变量添加动态验证 - use ngFor loop in Angular 6 to dynamically create array of input elements and add dynamical validation based on template reference variables 带有* ngFor的Angular 2模板引用变量 - Angular 2 template reference variable with *ngFor 如何在循环内的 Angular 模板上的 div 中动态命名引用 - How to name dynamically a reference in a div on Angular template inside a loop 如何在 Angular 中使用模板文字和 *ngFor 创建动态 html 模板 - How to create a dynamic html template with template literals and *ngFor in Angular Angular 2 - 动态创建/注入组件到* ngFor循环内的特定元素 - Angular 2 - Dynamically create/inject component into specific element inside *ngFor loop 使用 ngFor 的材料芯片输入 - 模板参考循环 - Material Chip Input with ngFor - template reference loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM