0

I have an Angular 6 project which I receive a JSON object like the following: json array

And now I want to inject/pass this on a html table. Here is my code:

Interface

interface Ilivelog {
  instID: string;
  procID: string;
  threadNum: string;
  status: string;
}

The object

dataAgent: Ilivelog;

Method

onGetAgent() {
  this.backend.getAgentStatus().subscribe(
    (response: Response) => {
      this.dataAgent = response.json();
      console.log("Arrey Agent: " + this.dataAgent);
    },
    (error) => {
      console.log(error)
    }
  )
}

How I get the data

getAgentStatus() {
  return this.http.get('http://localhost:8081/rms_agent/status');
}

How can I pass the this json in to a HTML table?

3 Answers 3

2

Try this in your Component Template:

<table border="1">
  <thead>
    <td>instID</td>
    <td>procID</td>
    <td>threadNum</td>
    <td>status</td>
  </thead>
  <tbody>
    <tr *ngFor="let item of dataAgent">
      <td>{{item.instID}}</td>
      <td>{{item.procID}}</td>
      <td>{{item.threadNum}}</td>
      <td>{{item.status}}</td>
    </tr>
  </tbody>
</table>

Here's a Sample StackBlitz for your ref.

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

12 Comments

didn't help, I got only the thead. I belive that this can't recognise the item.body.
it says: Arrey Agent: {"instID":"2018#11#06","procID":1161005,"threadNum":0,"status":9},{"instID":"2018#11#06","procID":1161030,"threadNum":0,"status":9},{"instID":"2018#11#06","procID":1161020,"threadNum":0,"status":9}
can you help me? There is no result displayed. And the log I have: Arrey Agent: {"instID":"2018#11#06","procID":1161010,"threadNum":0,"status":9}
can it be that I need to use httpClientModule on the back end or something?
import { Http } from "@angular/http"; constructor(private http: Http) { }
|
1

Check first if the JSON is valid in a validator.

1 Comment

Yes, this is was the main problem, my backend team was sending me a string not a json...
-2

In your component.ts:

results$: Observable<Ilivelog[]>;
ngOnInit() {
   this.result$ = this.backend.getAgentStatus().pipe(
        map(res => res.json())
   );
}

in your view.html

<table>
    <tr *ngFor="let log of (results$ | async)">
       <td>log.instID</td>
       <td>log.procID</td>
       <td>log.threadNum</td>
       <td>log.status</td>
    </tr>
</table>

4 Comments

Uncaught Error: Template parse errors: Can't bind to 'ngForIn' since it isn't a known property of 'tr'. ("
and then it shows only: log.instID log.procID log.threadNum log.status
the .pipe(map(res => res.json())); should be after the this.http.get() not instead of a .subscribe()
If you see getAgentStatus() return an observable. But if you use HttpClient module you don't need to do res.json().

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.