0

This is the response that I receive from the api in JSON

{
   "content":[
      {
         "id":"4354345",
         "name":"asdasd",
         "stuff":null
      },
         {
         "id":"4353345",
         "name":"fgdfgddd",
         "stuff":null
      },
         {
         "id":"4353425",
         "name":"as11111dasd",
         "stuff":null
      }

   ],
   "pageable":{
      "sort":{
         "sorted":false,
         "unsorted":true,
         "empty":true
      },
      "offset":0,
      "pageNumber":0,
      "pageSize":1000,
      "paged":true,
      "unpaged":false
   },
   "totalPages":1,
   "totalElements":75,
   "last":true,
   "number":0,
   "sort":{
      "sorted":false,
      "unsorted":true,
      "empty":true
   },
   "size":1000,
   "first":true,
   "numberOfElements":75,
   "empty":false
}

My objective is to itenerate the "Content" part of the JSON,the

"id":"4354345", "name":"asdasd", "stuff":null

in a table like this:

     <tbody>
          <tr *ngFor="let data of dataContent; index i">
                          {{data[i].id}}
                          {{data[i].name}}
                          {{data[i].stuff}}
          </tr>
      </tbody>

I'm doing it as follows:

 ngOnInit(){
       this.api.getAll((error,res) => {
           this.data = res;
           this.dataContent = this.data.content;
      });
  }

if I do a console.log of dataContent[0].id or other variables, it works, but why doesn't it show anything in the html and the console is giving errors

2
  • Please paste the error Commented Mar 2, 2021 at 18:38
  • What are those errors? Try debugging it or post it here. Commented Mar 2, 2021 at 18:38

3 Answers 3

1
<tbody>
          <tr *ngFor="let data of dataContent">
                          {{data.id}}
                          {{data.name}}
                          {{data.stuff}}
          </tr>
      </tbody>

No need to use index and I here

Or if you want I you have to

<tbody>
          <tr *ngFor="let data of dataContent; let i = index">
                       <td>   {{data.id}}</td>  
                       <td>     {{data.name}}</td>  
                      <td>      {{data.stuff}}</td>  
          </tr>
      </tbody>
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to access the index in this case, when you use *ngFor directive, you're looping dataContent and let data refers to one value in each iteration. Just:

<tr *ngFor="let data of dataContent">
    <td>{{data.id}}</td>
    <td>{{data.name}}</td>
    <td>{{data.stuff}}</td>
</tr>

If you really need to use the index (for any other case), the issues is the index i part, correct it to index as i (notice the missing as)

<tr *ngFor="let data of dataContent; index as i">
    <td>{{data[i].id}}</td>
    ...
</tr>

Again, you don't need the index in this case, at least not for the portion of code you shared.


Learn more about *ngFor here.

Comments

0

The HTML template needs to be corrected, data variable in the loop will hold one object in the array.

<tbody>
      <tr *ngFor="let data of dataContent">
          <td>{{data.id}}</td>
          <td>{{data.name}}</td>
          <td>{{data.stuff}}</td>
      </tr>
</tbody>

Comments

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.