2

I started learning AngularJS a few days ago, and I wonder how do I get the name of a field in a JSON file.

{contador: "1159273", Code: "TCP_MISS/200", $$hashKey: "object:12"}

Thats one line of the json file, and I want to get the "contador" and "Code". My objective is to do a table with that on top.

Here is my HTML code:

<table class="table">
    <tr >
      <td ng-repeat="(key, name) in $ctrl.logs.data">{{key}}</td>
    </tr>
    <tr ng-repeat="linha in $ctrl.logs.data">
      <td>{{ linha.contador }}</td>
      <td>{{ linha.Code }}</td>
    </tr>
  </table>

The "key" returns 0 1 2 ... and I want to get "contador" and "Code".

2
  • But is it impossible to get the names? Commented Jul 5, 2018 at 11:13
  • where is $ctrl.logs.data Commented Jul 5, 2018 at 11:14

1 Answer 1

1

It is because your data is an array change this line

 <td ng-repeat="(key, name) in $ctrl.logs.data">{{key}}</td>

with the following

<td ng-repeat="(key, name) in $ctrl.logs.data[0]">{{key}}</td>

Demo

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

3 Comments

it's possible to make it even more "automated"? Because I have other route that the JSON has other names, and I want to use the same page. {{ linha.Code }} is for this JSON, but I have other one that is {{ linha.Method }}.
Ok... I just did It... Thanks for the help :)
I am glad I was able to help.

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.