Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

angularjs - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...