csvangularjs

How to load CSV file to object in angualrjs?


In angularjs I created some service that loads JSON object:

// used to load table from json file instead ajax
myModule.factory('Items', ['$http', function($http){
    var Url   = "src/utils/some.json";
    var Items = $http.get(Url).then(function(response){
     return response.data;
  });
  return Items;
}]);

And in controller I can call it like:

Items.then(function(data){
  $scope.items = data;
});

As You can see I load some.json file.

What should be flow to do the same with CSV file?

in my case $scope.items is a list of objects.

Does someone know how to get CSV data by using $http.get or other way?


Solution

  • You have to parse the CSV file into an array. You can see this question for some alternatives: Javascript code to parse CSV data

    Then you'll end up with something like this:

    myModule.factory('Items', ['$http', function($http){
      var Url   = "src/utils/some.csv";
      var Items = $http.get(Url).then(function(response){
         return csvParser(response.data);
      });
      return Items;
    }]);