0

I am trying to add new column dynamically in the table. I have below object which is returned in response from web service api. How can I display dynamic columns where myArray object is getting appended with many other fields (new columns)

 $scope.myArray =  [
    {
  "contacts": [
    {
      "id": "1",
      "contact": {
        "name": "Sam",
        "Email": "[email protected]",
        "PhoneNo": 2355
      }
    },
    {
      "id": "2",
      "contact": {
        "name": "John",
        "Email": "[email protected]",
        "PhoneNo": 2355
      }
    }
  ]
}
  ];

I have tried looking into the example provided in some of the answers in google. In my below JSFIDDLE, I am getting only the key name but not the values. How can I achieve both key and value using ng-repeat where the key names are dynamic.

1 Answer 1

1

angular.module('myApp', [])
  .controller('myController', function($scope) {
    $scope.myArray =  [
    {
  "contacts": [
    {
      "id": "1",
      "contact": {
        "name": "Sam",
        "Email": "[email protected]",
        "PhoneNo": 2355
      }
    },
    {
      "id": "2",
      "contact": {
        "name": "John",
        "Email": "[email protected]",
        "PhoneNo": 2355
      }
    }
  ]
}
  ];
  });

angular.element(document).ready(function() {
  angular.bootstrap(document, ['myApp']);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
<table ng-controller="myController" border="1">
  <tr>
    <th ng-repeat="(key, val) in myArray[0].contacts[0].contact">{{ key }}</th>
  </tr>
  <tr ng-repeat="row in myArray[0].contacts">
    <td ng-repeat="(key, val) in row.contact">{{ val }}</td>
  </tr>
</table>

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

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.