简体   繁体   中英

Read and parse CSV file in AngularJs

File pattern

"A","Arabia"
"B","Brazil"
"C","Canada"
"D","Denmark"
"E","England"
"F","Finland"
"G","Germany"

User can select a file using input file type and i have to convert it into a json object like

$scope.dataList = [{ 
 Name: "A", 
 Value:"Arabia",
 IsEditing: false,
 Status: "unchanged",
 IsValueValid: true,
 IsNameValid: true 
}];

I found a lot about this but those are in jQuery. It is bad practise to use jQuery in AngularJs controller ... I'm new to AngularJs / Javascript field ..

Can anyone please help me to give suggestion about how should I do it...

Either write the parser yourself or use a third party to do it. If jQuery provides what you need, use it.

It's bad practice to do DOM manipulation on the controller, which is the common use case for jQuery, other use cases are fine in my opinion.

However, instead of using it straight in the controller, you can wrap the call to the CSV converter within a service. So in pseudo code, what you're looking for is:

angular.service('CSVConverterService', [function () {

    this.convertToArray = function (csvString) {              
           // Your parser logic here or call to the third party
    };

}]);

And in your controller you'll do:

CSVConverterService.convertToArray($scope.myCSVString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM