Fetch And Loop Over Unknown Objects In Angularjs
I've been working on generics in ASP.NET MVC for quite some time, and I've thought about generics on other languages, particularly in AngularJS. Suppose I have 2 sample endpoints:
Solution 1:
If all you're asking is how to get the property names for table headers, something like this should suffice (assuming each post has the same keys)
$http.get("http://jsonplaceholder.typicode.com/posts").then(function(response) {
$scope.data = response.data;
$scope.headers = Object.keys(response.data[0] || {});
});
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
Then you can use ng-repeat
<table><thead><tr><thng-repeat="header in headers">{{::header}}</th></tr></thead><tbody><trng-repeat="item in data"><tdng-repeat="prop in headers">{{::item[prop]}}</td></tr></tbody></table>
The nested loop is to maintain the key order established in headers
.
Post a Comment for "Fetch And Loop Over Unknown Objects In Angularjs"