简体   繁体   English

在AngularJs中读取和解析CSV文件

[英]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 用户可以使用输入文件类型选择文件,我必须将其转换为json对象,例如

$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. 我发现了很多有关这些的内容,但是这些都在jQuery中。 It is bad practise to use jQuery in AngularJs controller ... I'm new to AngularJs / Javascript field .. 在AngularJs控制器中使用jQuery是不好的做法...我是AngularJs / Javascript领域的新手..

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. 如果jQuery提供了您所需的内容,请使用它。

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. 在控制器上执行DOM操作是一种不好的做法,这是jQuery的常见用例,我认为其他用例都很好。

However, instead of using it straight in the controller, you can wrap the call to the CSV converter within a service. 但是,您可以将调用包装到服务中的CSV转换器,而不是直接在控制器中使用它。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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