简体   繁体   English

将lodash注入Angular provider

[英]Injecting lodash into Angular provider

I've seen several questions about injecting lodash/underscore into controllers, but I need to put it into a custom provider. 我已经看到了关于将lodash / underscore注入控制器的几个问题,但是我需要将其放入自定义提供程序中。 I can't seem to figure out how to do that. 我似乎不知道该怎么做。 There is a similar question here that has an accepted answer, but I can't figure out how to re-purpose that technique for my situation. 这里有一个类似的问题 ,已经得到了公认的答案,但是我不知道如何针对我的情况重新使用该技术。

I'm injecting lodash into angular as a separate module: 我将lodash作为单独的模块注入angular中:

angular.module("lodash", [])
.factory("_", ($window) => {
    return $window._;
});

How would I modify or add to this in order to make lodash injectable into my provider? 我将如何修改或添加它,以使lodash可注射到我的提供程序中?

To inject Lodash to both service providers and service instances use 要将Lodash注入服务提供商和服务实例,请使用

angular.module("lodash", [])
.constant("_", window._);

instead . 相反。 There are scenarios where using $window abstraction may be beneficial, but here it can be safely omitted. 在某些情况下,使用$window抽象可能是有益的,但是在这里可以安全地省略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM