您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Angular控制器中使用下划线

在Angular控制器中使用下划线

当您包含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
});
其他 2022/1/1 18:14:33 有471人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶