PapaParse with Angular JS

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 08:59:31

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.

Francisco López-Sancho

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);
    }
});

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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!