eddie eddie -4 years ago 152
TypeScript Question

Typescript JSON

My question can be stupid but I have to post.I am totally new to Typescript, I have this query that I have JSON data as below:
[{"regionID":1 "regionname":"Can"},

{"regionID":2 "regionname":"Cen"}]

I want to fetch the above data using typescript and display it in table format like:

region_id | Region name
1 | CAN
2 | CEN


Can anyone help me in this ,
Any help Appreciated

Thanks in advance.

Answer Source

Try this,

export class MyComponent{

   myData:any; ///Declare Variable

}

Service:

// Suppose your data is returned this way.
get(url) {
   return this._http.get(url, { headers: headers })
        .map(res => res.json())
        .subscribe
         (
         data => {
            this.myData = data;
         });
}

HTML:

<table>
    <thead>
        <th>region_id </th>
        <th>Region name</th>
    </thead>
    <tbody>
        <tr *ngFor="let item of myData">
            <td>{{item.region_id}}</td>
            <td>{{item.Regionname}}</td>
        </tr>
    </tbody>
</table>

Hope it helps,

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download