PapaParse with Angular JS

All we need is an easy explanation of the problem, so here it is.

Liked the nice CSV parser & unparser of PapaParse. Can any one help me to get this combine with Angular JS.

I like to make PapaParse work in Angular Way. Trying for a solution.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I actually didn’t do anything fancy to load it. Just add it to html file and to my lib folder. In my case: /lib/papaparse.min.js

and to index.html. As usual script:

<script src="lib/papaparse.min.js"></script>

then I just used it in my Controller:

Papa.parse(data, {
    complete: function(results) {
        console.log("Finished:", results.data);
    }
});

Method 2

You can use value to provide self contained third party libraries.

angular.module('your.app')
    .value('yourLib', yourLib);

Then in your controller, or service, you would bring it in the normal way using DI

angular.module('your.app')
    .controller('YourController', YourController);

YourController.$inject = ['yourLib'];
function YourController(yourLib) {

   //. . .  
} 

If the third party line is a constructor function, requires it be newed, you may want to create a factory or a provider that has a method that accepts the passes params to the constructor returns a new instance.

Edit

After looking at PapaParse, you would want to register it with the angular injector using value.

Method 3

just use a front-end modularization tool like requirejs to load papaParser in the context and call the api in any of your controller or service.

Method 4

Just inject the script url in your index.html & then in your controller, access it as – var Papa = window.Papa;. That’s it! You are ready for further actions!

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply