2016-08-24 60 views
1

我試圖巢* ngFor這樣的:範圍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?

+0

cubeArray被綁定到特定的div和嵌套的for是可能 – rashfmnb

+0

檢查此例如http://run.plnkr.co/plunks/gvEQDugoQ1OQPvsZXuuM/ – rashfmnb

回答

2

您面臨的問題是什麼?

我在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(){} 
} 

看一看。

希望這有助於!

+0

實際上,這不會將值傳遞到組件的輸入。我修改了你的plunkr來顯示它:https://plnkr.co/edit/thAs3Fwf2rkRMn4aIjbv?p=preview – Ced

+1

你必須研究你創建數組的方式,它沒有被正確定義,因此你得到了iussues,[updated你的Plunker](https://plnkr.co/edit/DrAL2a4bdVCt5H3boipY?p=preview),乾杯! –

1

cubeArray被綁定到特定的div和嵌套ngFor是可能

檢查此Plunker