2

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.

5
  • 1
    could you clarify more on PapaParse. usually you could convert any third-party lib into angular service to have easy injection into angular context. Commented Dec 30, 2014 at 6:53
  • Is it good to have lib call in controller or need to have a directive for importing file. I'm confused on many other things too Commented Dec 30, 2014 at 7:15
  • i would prefer to directly use it in controller, directive is mostly used when you want a DOM created Commented Dec 30, 2014 at 7:19
  • github.com/stevemao/angular-PapaParse Commented Jun 14, 2015 at 22:13
  • github.com/Alberthaff/ngx-papaparse Commented Apr 4, 2018 at 13:23

4 Answers 4

7

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

1 Comment

This approach works fine unless you need to stub or mock PapaParse out for unit testing of your controller. You don't want to get into a situation where you are monkey patching in your test setup.
4

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.

5 Comments

agree with you. Will be helpfull with a example (a fiddle)
@felix, unfortunately Papaparse isn't hosted anywhere with the correct MIME type -- Stackoverflow, Plunkr and Jsfiddle require the libs have the correct MIME.
noprops, I got a similar example with another library in SO stackoverflow.com/questions/14968297/…
@Felix, yes this approach is the same. The only difference is he is using a factory rather than a value. Value is a lighter approach, but both do the same thing.
0

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.

1 Comment

I had completed my application, just for csv import, i had already need PapaParse so i don't like a new tool to be added. anyway Thanks.
0

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!

Comments

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.