javascript - angularjs, browserify + underscore -



javascript - angularjs, browserify + underscore -

i have below on angularjs app works browserify + gulp.

require('angular'); require('angular-resource'); require('angular-xeditable'); require('angular-spinner'); var app = angular.module('itembase', [ require('angular-ui-router'), 'ngresource', 'xeditable', 'angularspinner' ]);

my question on how integrate underscore library environment. tried few ways like, putting require('underscore'); above var _ = require('underscore');, doesn't seem work. files in code visible in angularjs app.

first of underscore not angular module. solution load underscore (with browserfy: require('underscore')) , create mill returns underscore , makes injectable in other angular components:

angular.module('itembase') .factory('_', function($window) { homecoming $window._; });

after this, need inject in component, other:

angular.module('itembase') .controller('mycontroller', function(_) { _.each([1,2,3,4], function() { console.log('using underscore!!!'); }); });

the beauty of approach dependency quite decoupled code , easy test or alter without having rewrite stuff later.

for example, if @ end of project realize utilize underscore iterating on arrays, reimplement _ mill like:

angular.module('itembase') .factory('_', function($window) { homecoming { each: function(el, fn) { for(var = 0; < el.length; i++) { fn(el[i], i, el); } } }; });

javascript angularjs browserify

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -