0

What i want to achieve is use the JSON details and populate the html table with 2 rows of 3 records.

My html file is as follows

<!DOCTYPE html>
<head>
    <title>DETAILS</title>
    <link rel="stylesheet" href="css/style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script src="js/main.js"></script>
</head>
<body ng-app="pr_app">
    <h2>DETAILS</h2>
    <div ng-controller="view_controller as view1">
    <table>
        <thead>
            <caption> PR DETAILS </caption>
        </thead>
        <tbody>
            <tr>
                <th>Product</th>
                <th>Quantity</th>
                <th>Owner</th>
            </tr>
            <tr ng-repeat="items in view1.values">
               <td>{{ items }}</td>
            </tr>
        </tbody>
    </table>
    </div>
</body>

MY JS file that houses the module and controller are as follows. NOTE: My first console.log() statement logs "(2) [Object, Object]" which means i am getting the response from my JSON file but the second console.log() gives me '6 undefined' so I cannot access the values of properties in my JSON array.

Can anyone point me is my JSON wrong or my http.get is doing something unexpected or anything else is wrong?

var app = angular.module("pr_app",[]);
app.controller("view_controller",['$http', function($http){
var self = this;
var record=[] , values=[];
$http.get('js/details.json').then(function(res){
    self.record = res.data;
    console.log(self.record);
    for(var i = 0; i< self.record.length; i++)
    {
        for(var key in self.record[i])
        {
            self.values = record[key];
            console.log(self.values);
        }
    }
}, function(){
    console.log("Something went wrong");
})
}]);

And my JSON is as below :

[
    {
        "product": "Adobe",
        "quantity": "1",
        "owner": "ABC"
    },
    {
        "product": "Adobe",
        "quantity": "1",
        "owner": "XYZ"
    }
]
  • Store reference to controller `this` and use that reference inside callbacks. See [John Papa Angular Style Guide](https://github.com/johnpapa/angular-styleguide/tree/master/a1#style-y032) – charlietfl Jun 25 '17 at 11:27
  • Also logic doesn't make sense in your `for()` loop where you overwrite same variable every iteration – charlietfl Jun 25 '17 at 11:28
  • @charlietfl the for loop is only used to access the property values inside the response. Can you point what or where am i going wrong and why i cannot populate my table? I took heed to your first comment. Since you marked this as a duplicate can you link me to more questions of this exact nature! – Ajitesh Chhatani Jun 25 '17 at 12:00
  • You need to start with understanding what `this` is inside the callback – charlietfl Jun 25 '17 at 12:01
  • @charlietfl I will but can you help or at least tell me how everything is right except getting 'undefined' for property values? – Ajitesh Chhatani Jun 25 '17 at 12:14
  • I suggest you study some angular tutorials for `ng-repeat`. To be honest none of what you are doing with the response data makes sense but the json structure does make sense – charlietfl Jun 25 '17 at 12:15

0 Answers0