How to use underscore.strings with AngularJs

1k views Asked by At

I'm having trouble getting underscore.strings working with AngularJS.

The documentation around underscore.strings always shows examples with requireJs which I don't think is appropriate for AngularJS.

I can't access the string functions with any of these statments, I'm a little confused on how I'm supposed to initialize underscore.strings without requirejs

<script src="/bower_components/underscore/underscore.js"></script>
<script src="/bower_components/underscore.string/dist/underscore.string.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        try {
            console.log(_); // Returns a reference to Underscore

        } catch (e) {
            console.log(e); 
        }
        try {
            console.log(_s); // Undefined

        } catch (e) {
            console.log(e);
        }
        try {
            console.log(_.str); // Undefined
        } catch (e) {
            console.log(e);
        }
        try {
            console.log(_.string); // Undefined
        } catch (e) {
            console.log(e);
        }
    });

</script>
2

There are 2 answers

0
AudioBubble On BEST ANSWER

Try using s to access the underscore.string global. Integrate into underscore with _.mixin(s.exports());

2
nweg On

Try using this:

https://github.com/floydsoft/angular-underscore

This module exposes underscore's API into angular app's root scope, and provides some filters from underscore.

Use it in conjunction with:

https://github.com/epeli/underscore.string