floydwch / angular-underscore

Underscore adapter for AngularJS
316 stars 86 forks source link

load into controller? #14

Open ralyodio opened 10 years ago

ralyodio commented 10 years ago

How do I load this thing into my controller so I get _?

floydwch commented 10 years ago

Do you mean https://github.com/floydsoft/angular-underscore#load-api-only ?

sandrocsimas commented 10 years ago

Yes, but how to use ?

ralyodio commented 10 years ago

I ended up just doing this:

    .run(function ($rootScope, $location, Auth) {

        //expose lodash to templates
        $rootScope._ = _;

        });
    });
sandrocsimas commented 10 years ago

Another way:

var underscore = angular.module('underscore', []);
underscore.factory('_', function() {
    return window._;
});  
screendriver commented 10 years ago

Can this be done in the angular-underscore library? I would like to use it like so:

angular.module('myApp', ['angular-underscore'])
.controller('MyCtrl', function($scope, _) {

});
jarcoal commented 10 years ago

It does seem a little silly that you can't just have underscore injected into a controller.

@floydsoft would it be reasonable to add in something like this:

.constant('_', _)

I could submit a PR if you'd accept it.

floydwch commented 10 years ago

@chovy @sandro-csimas @ScreenDriver : Oops, I finally found that I forgot adding document of how to access the underscore utilities from controller, in the current version, the utilities is bound on the $rootScope which means we can access the functions of underscore by $scope such as:

angular.module('myApp', ['angular-underscore'])
.controller('MyCtrl', function($scope) {
    $scope.sample([1, 2, 3]); // got 1, or 2, or 3.
    $scope._([1, 2, 3]); // underscore's chain, http://underscorejs.org/#chain
});
floydwch commented 10 years ago

@jarcoal PR is welcome!

sandrocsimas commented 10 years ago

Thanks @floydsoft, i will check later!