Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load CSV file to object in angualrjs?

Tags:

angularjs

csv

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?

like image 289
Maxim Shoustin Avatar asked Jun 05 '13 07:06

Maxim Shoustin


People also ask

What is AngularJS used for?

AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. AngularJS's data binding and dependency injection eliminate much of the code you would otherwise have to write.


1 Answers

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;
}]);
like image 198
joakimbl Avatar answered Oct 03 '22 17:10

joakimbl