Lodash and underscore.string into AngularJS application

915 Views Asked by At

The question is simple, how to successfully implement Lodash with underscore.string library into AngularJS project?

It is necessary to use RequireJS to achieve this?

3

There are 3 best solutions below

0
sylwester On BEST ANSWER

Yuo can use angular-underscore

please see here https://github.com/floydsoft/angular-underscore

or angular-lodash

https://github.com/cabrel/angular-lodash

2
Egel On

Thanks to @sylwester reply I figured it out, but his answer was just a hint, so I decided to enrich it a bit more.

It is necessary to use RequireJS to achieve this?

No, you don't need to use RequireJS to implement both libraries together into AngularJS project. But it can be easily achieved - just look at this article.

Implementation without RequireJS

  1. Use angular-lodash or angular-underscore (depends what's your implementation)
  2. Then download underscore.string to your project:

    $ bower install underscore.string --save-dev
    
  3. Finally make sure that libraries were added to your index.html:

    <script type="text/javascript" src="vendor/loadash/lodash.js"></script>
    <script type="text/javascript" src="vendor/angular/angular.js"></script>
    <script type="text/javascript" src="vendor/angular-loadash/angular-loadash.js"></script>
    <script type="text/javascript" src="vendor/underscore.string/underscore.string.js">
    

Feel free to improve this answer with RequireJS.

0
Vicheanak On

create this service and now you can use underscore.string in underscore.js

something like: _.capitalize('hello') --> 'Hello'

angular.module('underscore', []).factory('_', ['$window', function($window) {
    $window._.mixin(s.exports());
    return $window._;
}]);