0

I am building my first ever website in angularJs. Currently, trying to load a table using the following code

$scope.voyages = [
                {
    "data1": "data",
    "data2": "data
}, {
    "data1": "data",
    "data2": "data
}];

but I want to put the data from my file.json into this table, how can I do it? I've tried the getJson function of jQuery but had no success, I might not be using it the correct or recommended way.

EDIT : Fixed it like this :

 function VoyagesCtrl($scope) {
       $scope.voyages= $.get( "file.json", function(data){
       $scope.voyages = JSON.parse(data);
        console.log($scope.voyages);
        });
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
thomasA
  • 257
  • 1
  • 4
  • 11

2 Answers2

0

There's no absolute correct way to do it (depends on the situation). but I think what you're trying to do is use the data to locally test your angular app, for that you can check this answer here which is pretty much the same situation. https://stackoverflow.com/a/18060563/7988438

What I'd advice though, is setting up a local server (using Node and Express) to serve your data, so you don't need to change the API calls once you're done with local testing, (use this if you're lazy, or if back-end isn't your thing), and getting them using AngularJS's $http.get.

0
var table_data =  $http.get('path-to-local-json', {}, 
{cache:false}).then(function(result){

       //return the data to the controller
       return result;
},function(err){
      //Handle errors.
});
DudeM
  • 23
  • 4