1

I am trying to dynamically create a key inside the template from an *ngFor index.

<div *ngFor="let col of table; let i = index">
  <span *ngFor="let row of col; let j = index">
    {{ row.ricj }}
  </span>
</div>

where the "i" and "j" in "ricj" are actually the indices "i" and "j". Is this possible?

The original table object is build this way,

makeTable(){
    for(this.c = 0; this.c < this.columnslength; this.c++){
      for(this.r = 0; this.r < this.rowslength; this.r++){
        this.key = "r" + this.r + "c" + this.c;
        this.cell = { [this.key]: this.key };
        this.cols.push(this.cell);
      }
      this.table.push(this.cols);
      this.cols = [];
  }
}

the table object looks like this,

[
   {
      [{ "r0c0": "r0c0" }, { "r1c0": "r1c0" }]
   },
   {
      [{ "r0c1": "r0c1" }, { "r1c1": "r1c1" }]
   }
]
2
  • what is the issue? Commented Jun 30, 2017 at 17:18
  • I want the key in each loop to be a concatenation of the indices. Commented Jun 30, 2017 at 17:22

1 Answer 1

3

Try this,

  <div *ngFor="let col of table; let i = index">
    <span *ngFor="let row of col; let j = index">
        {{ row['r' + j + 'c' + i] }}
    </span>
  </div>
Sign up to request clarification or add additional context in comments.

2 Comments

This is very interesting but unfortunately gives an error. HighlightTableComponent.html:6 ERROR TypeError: Cannot read property '0' of undefined
You're a genius, this works . {{ row['r' + j + 'c' + i] }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.