1

edit: I've updated the code with the latest changes you guys proposed.

I have this data.json:

[{
"codigo": 21420,
"desc": "itv orion 20 a",
"prod_24_h_kg": 22
}, {
"codigo": 21421,
"desc": "itv orion 20 w",
"prod_24_h_kg": 24
}, {
"codigo": 21430,
"desc": "itv orion 30 a",
"prod_24_h_kg": 31
}, {
"codigo": 21431,
"desc": "itv orion 30 w",
"prod_24_h_kg": 34
}, {
"codigo": 21440,
"desc": "itv orion 40 a",
"prod_24_h_kg": 40
}, {
"codigo": 21441,
"desc": "itv orion 40 w",
"prod_24_h_kg": 42
}, {
"codigo": 21460,
"desc": "itv orion 60 a",
"prod_24_h_kg": 54
}, {
"codigo": 21461,
"desc": "itv orion 60 w",
"prod_24_h_kg": 56
}, {
"codigo": 21480,
"desc": "itv orion 80 a",
"prod_24_h_kg": 77
}, {
"codigo": 21481,
"desc": "itv orion 80 w",
"prod_24_h_kg": 79
 }, {
"codigo": 21490,
"desc": "itv orion 130 a",
"prod_24_h_kg": 130
}]

In app.js I have this:

$scope.people = [];
$scope.loadData = function() {
  alert("2");
  $http.get('data.json').then(function(data) {
    alert("succ");
    console.log(data)
    $scope.people = data.maq;
  });
};

Also, this is the HTML:

<div>
  <p> Click <a ng-click="loadData()">here</a> to load data.</p>
  <table>
    <tr>
      <th>Id</th>
      <th>First Name</th>
      <th>Last Name</th>
    </tr>
    <tr ng-repeat="person in people">
      <td>{{person.codigo}}</td>
      <td>{{person.desc}}</td>
      <td>{{person.prod_24_h_kg}}</td>
    </tr>
  </table>
</div>

So, when I click I do get the alert from the function, however the data won't load; I get no errors either.

I'm not sure what I'm missing. I thought it wa a cross origin problem, but the json is on the same domain and I don't get the error. I'm running a local server with XAMPP.

10
  • 1
    Where does the 'data.json' is located ? Commented Sep 13, 2016 at 8:48
  • 3
    You can add line console.log("data") inside callback function. This way you would see in console what is actually being loaded. Commented Sep 13, 2016 at 8:50
  • 1
    Accessing local file with $http.get Commented Sep 13, 2016 at 8:53
  • 1
    What response do you get? Commented Sep 13, 2016 at 8:55
  • 1
    it's not part of your issue but you should replace .success (deprecated) to .then Commented Sep 13, 2016 at 8:59

2 Answers 2

2

<table ng-if="people">

And based on your last print there's a data inside your object so it should be:
$scope.people = data.data.maq;

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

1 Comment

data.data did the trick, I thank you very much for seeing it and helpig me out. I'll mark this as the correct answer. Thanks to everyone else as well. I'll update the code ASAP. Didn't add the maq, though.
1

Your JSON is invalid. line 101 "codigo": 21441, might start with { ? verify with some online json validor http://jsonlint.com/

1 Comment

Thank you for your answer. The JSON is valid, I just cut it down short and made a mistake. I validated it on jsonlint.com.

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.