Javascript – Importing plain javascript libraries in an AngularJs application

angularjsgithubjavascriptlibraries

I am working on an AngularJs project which is getting bigger very fast.

Currently I need to store some user data, so I was using $localStorage for Angular.

But I had a problem, as the application is saving a big amount of data, it is hanging the browser some seconds which is very annoying.

Searching for asynchronous uses of localStorage I've found a bunch of people saying that it not possible to use it. But I managed to found some plain javascript implementations on GitHub:

There is an Angular implementation which is based on what Mozilla provides, but the build is failing and I want to use something more stable due to the importance of the project.

Then I came to this doubt, how should I use plain/vanilla javascript inside an Angular app?

I know that it is discouraged to use jQuery in the way people normally are "tempted" to use. How should I organize my pure javascript libraries in my application in a way that I can still see it very organized and fitting in the "Angular Way"?

EDIT: I know I have given you a fairly specific example about my data storage problem and some people might want help me with this, I really appreciate that (seriously). But unfortunately this is not the kind of answer I expect, since it does not have any relation with my real question. Please consider that even if I solve my data problem on my own, I will still want to know what are the best practices of including plain javascript code on my Angular Apps (that's the reason I've posted it here and not in StackOverflow). I could not find any good articles or questions here about it. I want the power of thinking of all great developers out there to help me, but most of all, help the community. I really think that good answers to this question could help a great amount of people, and that is what StackExchange is all about.

Best Answer

You might like Require.js. I don't believe there is a "standard Angular way" of importing code because they figure you'll use any of the other techniques:

  • Concatenate and minify using Bower
  • Import using regular script tags
  • Use a loading library like Require.js
  • Just define your own stuff as services in Angular

You also might want to read http://oblongmana.com/articles/angular-third-party-injection-pattern/ as it describes a pattern for Angularizing third party libraries, which are just plain old JavaScript.

Related Topic