1

I want to get data stored in template_A that is col_1 and col_2 and print in table format, but i am getting [object,object]. I don't know how to get data stored as array of object.

I want to print next to template_A. You can have a look at array in console.

enter image description here

enter image description here

{
    "_id": {
        "$oid": "611f354944c4571778df456c"
    },
    "assignmentId": "61235a452a8a2428a43514ed",
    "studentId": "60dafc5c0254f7122e9fe7f0",
    "assignmentOf": "subject",
    "createdAt": {
        "$date": "2021-08-20T04:53:29.717Z"
    },
    "updateAt": {
        "$date": "2021-08-20T04:53:29.717Z"
    },
    "__v": 0,
    "grade": "b ",
    "remark": "good",
    "template": "template_A",
    "template_A": {
        "col_1": "col_1",
        "col_2": "col_2"
    },
    "template_B": {
        "col_1": "col_1",
        "col_2": "col_2"
    }
}

Angular app

                          <table>
                            <tr >
                              <td>{{ singleAssignment.template }}</td>
                              <td>{{ singleAssignment.template_A[0] }}</td>
                              <!-- <td>{{ singleAssignment.template_A[0] }}</td> -->
                            </tr>
                          </table>

3 Answers 3

2

Two ways

1. Transform your json object to json array, and then use *ngFor in html.

"template_A": [
        { "col_1": "col_1" },
        { "col_2": "col_2" },
      ]

2. use Object.keys in *ngFor

"template_A": {
      "col_1": "col_1_value",
      "col_2": "col_2_value"
  }

in .ts file

public ObjectKeys = Object.keys

in .html file

<div  *ngFor="let cols of ObjectKeys(singleAssignment.template_A)">
   {{cols}}
   {{singleAssignment.template_A[cols]}}
</div>

output -

col_1 col_1_value
col_2 col_2_value
Sign up to request clarification or add additional context in comments.

Comments

1

I Have modified code for you please have a look

TS CODE

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  singleAssignment: any;

  constructor() {
    this.singleAssignment = {
      "_id": {
          "$oid": "611f354944c4571778df456c"
      },
      "assignmentId": "61235a452a8a2428a43514ed",
      "studentId": "60dafc5c0254f7122e9fe7f0",
      "assignmentOf": "subject",
      "createdAt": {
          "$date": "2021-08-20T04:53:29.717Z"
      },
      "updateAt": {
          "$date": "2021-08-20T04:53:29.717Z"
      },
      "__v": 0,
      "grade": "b ",
      "remark": "good",
      "template": "template_A",
      "template_A": {
          "col_1": "col_1",
          "col_2": "col_2"
      },
      "template_B": {
          "col_1": "col_1",
          "col_2": "col_2"
      }
    }
  }
}

HTML CODE

<table>
  <tbody>
    <tr >
      <td>{{ singleAssignment.template }}</td>
      <td>{{ singleAssignment.template_A['col_1']}}</td>
      <td>{{ singleAssignment.template_A['col_2'] }}</td>
    </tr>
  </tbody>
</table>

OUTPUT enter image description here

============================================================================

UPDATED

TS CODE

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  singleAssignment: any;

  constructor() {
    this.singleAssignment = {
      "_id": {
          "$oid": "611f354944c4571778df456c"
      },
      "assignmentId": "61235a452a8a2428a43514ed",
      "studentId": "60dafc5c0254f7122e9fe7f0",
      "assignmentOf": "subject",
      "createdAt": {
          "$date": "2021-08-20T04:53:29.717Z"
      },
      "updateAt": {
          "$date": "2021-08-20T04:53:29.717Z"
      },
      "__v": 0,
      "grade": "b ",
      "remark": "good",
      "template": "template_A",
      "template_A": [
        {
          "col_1": "col_1",
          "col_2": "col_2"
        }
      ],
      "template_B": {
          "col_1": "col_1",
          "col_2": "col_2"
      }
    }
  }
}

HTML CODE

<table>
  <tbody>
    <tr >
      <td>{{ singleAssignment.template }}</td>
      <td>{{ singleAssignment.template_A[0]['col_1']}}</td>
      <td>{{ singleAssignment.template_A[0]['col_2'] }}</td>
    </tr>
  </tbody>
</table>

OUTPUT enter image description here

11 Comments

@Suneelumar i tried your solution din't work for me.
can you please share your code? so that I can check
Like I also shared screenshot by running this above example, for me it's working fine, what issue you are facing?
` <table> <tr > <td>{{ singleAssignment.template }}</td> <td>{{ singleAssignment.template_A['col_1'] }}</td> <td>{{ singleAssignment.template_A['col_2'] }}</td> </tr> </table>` used this solution i am not getting any result. i can fetch template but not template_A
share your output screenshot and also code this singleAssignment
|
1

Use *ngFor directive on singleAssignment.template_A and then iterate its items.

1 Comment

@Amna.Naveed how are you suggesting t implement it in table fomat? Can you give example.

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.