当您包含Underscore时,它会将自己附加到window
对象上,因此在全局范围内都可用。
因此,您可以按原样从Angular代码使用它。
如果希望将其注入,也可以将其包装在服务或工厂中:
var underscore = angular.module('underscore', []);
underscore.factory('_', ['$window', function($window) {
return $window._; // assumes underscore has already been loaded on the page
}]);
然后,您可以_
在应用程序的模块中请求:
// Declare it as a dependency of your module
var app = angular.module('app', ['underscore']);
// And then inject it where you need it
app.controller('Ctrl', function($scope, _) {
// do stuff
});