VansFannel
VansFannel

Reputation: 45911

Using ngFor with an index representing the row where I am

I've just started to learn Angular 4 and I have trying to use this array:

public productsByLaw: IHash;
interface IHash {
    [row: number]: IProduct[];
}

The productsByLaw will store an IProduct array for each row I will display in a <table>.

In a ngFor (in the second one of this example):

<tr *ngFor="let productionorder of productionorders; let rowIndex = index">
<td>
    <select id="ProductId-{{rowIndex}}" name="ProductId-{{rowIndex}}" #productId="ngModel" [(ngModel)]="productionorders[rowIndex].productid" required>
        <option></option>
        <option *ngFor="let product of productsByLaw[{{rowIbdex}}]" [value]="product.productId">{{product.name}}</option>
    </select>
</td>

But I get a syntax error here: productsByLaw[{{rowIndex}}] with the {{rowIndex}}.

I want to use the IProduct[] array depending on the row where I am.

How can I do what I what to do?

Upvotes: 0

Views: 109

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657078

It should be

productsByLaw[rowIndex]

{{}} is only for string interpolation

Bindings with * (like in *ngFor="..." or [foo]="..." are evaluated as expressions and not strings.

Upvotes: 3

Related Questions