6

The following scenario would be pretty easy in javascript but I got some problems getting it worked in Angular.

I got an Array like:

array a = ( "id" = 0, name = random(), column = 1, 2 or 3, blockrow= 1, 2 or 3)

With ngFor I try to create now a grid there all elements out of a get separated in colums and blocks in this column. So my current code (works but nasty).

<div *ngFor="let a of a">
  <template [ngIf]="a.column=='1'">
    <div *ngFor="let b of a">
      <template [ngIf]="b.blockrow=='1'">
        Block 1 in column 1{{b.name}}
      </template>
    </div>
    <div *ngFor="let b of a">
      <template [ngIf]="b.blockrow=='2'">
        Block 2 in column 1{{b.name}}
      </template>
    </div>
    <div *ngFor="let b of a">
      <template [ngIf]="b.blockrow=='3'">
        Block 3 in column 1{{b.name}}
      </template>
    </div>
  </template>
</div>

Something like this do I run for every column. Which means I loop trough the same array for 12 times. Is there any way to do it more beautifull ?

2 Answers 2

9

In your component, use JavaScript to build an array of arrays with the elements of a at the right coordinates, then use *ngFor inside *ngFor:

<div *ngFor="let row of rows">
  <div *ngFor="let col of row">
    Block {{col.blockrow}} in column {{col.column}} {{col.name}}
  </div>
</div>

A third *ngFor might be needed if several blocks have the same coordinates.

Sign up to request clarification or add additional context in comments.

1 Comment

So I can work with multidimensionals Arrays this way. Perfect thank you this is exactly what I wanted.
0

You are using wrong syntax should be *ngIf instead of [ngIf]

<div *ngFor="let a of a">
  <template *ngIf="a.column=='1'">
    <div *ngFor="let b of a">
      <template *ngIf="b.blockrow=='1'">
        Block 1 in column 1{{b.name}}
      </template>
    </div>
    <div *ngFor="let b of a">
      <template *ngIf="b.blockrow=='2'">
        Block 2 in column 1{{b.name}}
      </template>
    </div>
    <div *ngFor="let b of a">
      <template *ngIf="b.blockrow=='3'">
        Block 3 in column 1{{b.name}}
      </template>
    </div>
  </template>
</div>

8 Comments

Changed it top, the editor uses angular 1, but this is not my problem. Just want to know if there is a way to run the loop not so often.
I didn't get you
In my example I loop trough the same array 4 times. To make it completly working I need this 3 times, which means 12 times looping trough the same array. In my eyes this is very inefficient. In plain JS I could do the same with going only once trough the array. So my question is, is there a way to do this block of code better ? Something like if b.blockrow == 1 -> add to div1, if b.blockrow == 2 add to div 2.
Btw. tried your change in my angular app with *ngIf it does not work. The documentation also says [ngIf] for template.
what is the exact problem.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.