Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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...

share|improve this question
    
won't find anything in angular specifically, try google, lots of javascript csv parsing info – charlietfl May 18 '14 at 13:14
up vote 2 down vote accepted

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);
share|improve this answer
    
thanks a lot.. :) ..u use service here .. it will help me to write service when i need.. it works as a demo for me.. i have another question.. do u plz tell me why u use $rootscope and $apply at the time of calling 3rd party .. @haimlit – Sadid Khan May 18 '14 at 14:55
    
After thinking about it for a bit, I was wrong, it's not needed here. Scope apply is needed when there is an update to scope bindings. Since we have non of that here (we only parse a string and return a new object) it's redundant. Here's a guide that explains a bit on when to use it: jimhoskins.com/2012/12/17/angularjs-and-apply.html – haimlit May 18 '14 at 15:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.