1

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

1
  • won't find anything in angular specifically, try google, lots of javascript csv parsing info Commented May 18, 2014 at 13:14

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.