【问题标题】:scope of *ngFor let variable*ngFor let 变量的范围
【发布时间】:2016-08-24 20:22:04
【问题描述】:

我试图像这样嵌套 *ngFor :

   <div *ngFor="let cubeArray of cubeMatrice">
    <my-cube *ngFor="let cube of cubeArray">
    </my-cube>
   </div>

但是这不起作用。变量 cubeArray 是否仅与特定的 div tag 相关联?

我还尝试了一些我在这里找到的其他解决方案:How to use Angular2 templates with *ngFor to create a table out of nested arrays?,但这不适用于原始 html 标签。

我知道我可以在两者之间使用一个组件来解决问题。我只想知道嵌套 fors 是否可能以某种方式或为什么不可行。 cubeArray 变量的作用域只是 div 吗?

【问题讨论】:

标签: angular


【解决方案1】:

您面临的问题是什么?

我在Plunker 中尝试过,它有效。

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

@Component({
  selector: 'my-app',
  template: `<h3 class="title">Basic Angular 2</h3>
  <hr />
  <div *ngFor="let cubeArray of cubeMatrice">
    <my-cube *ngFor="let cube of cubeArray" [var1]="cube.prop1" >
   </my-cube>
   </div>
  `
  })
  export class AppComponent {
   cubeMatrice = [
     [{prop1: 'val1'},{prop1: 'val2'}],
     [{prop1: 'val3'},{prop1: 'val4'}]
   ]

   constructor(){}
}

 @Component({
 selector: 'my-cube',
 template: `{{var1}}
  `
 })
 export class MyCubeComponent {
  @Input() var1;
  constructor(){}
 }

看看吧。

希望这会有所帮助!

【讨论】:

【解决方案2】:

cubeArray 绑定到特定的 div 并且可以嵌套 ngFor

查看Plunker

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 2012-12-28
    • 1970-01-01
    • 2019-04-05
    • 2013-12-27
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2020-08-10
    相关资源
    最近更新 更多