简体   繁体   English

如何在配置函数中访问服务或访问服务中的提供程序

[英]How can I access a service in config function or access a provider inside a service

I need to configure angular bootstrap tooltip (uibTooltip) to be disabled for mobile devices when using angular-bowser for device detection. 我需要配置角度引导程序工具提示(uibTooltip)以在使用angular-bowser进行设备检测时为移动设备禁用。

This could be done simply by: 这可以通过以下简单的方法完成:

isMobile = _bowser.mobile
$uibTooltipProvider.options = { trigger: isMobile ? "none" : "mouseenter" }

Problem: $uibTooltipProvider is a provider and bowser is a service . 问题: $uibTooltipProvider提供者bowser服务

I have to use $uibTooltipProvider in a config function while I can't use bowser service in a config function. 我必须使用$uibTooltipProviderconfig功能,而我不能使用bowser在服务config功能。 And neither I can use $uibTooltipProvider in a run function where I can use bowser 而且,无论我可以用$uibTooltipProviderrun功能,我可以用bowser

I have already tried overriding the $get function as they suggest here but the "ontouchstart" event in $window does not apply for tablets where I want to keep tooltips enabled. 我已经尝试重写得到$功能,因为他们认为在这里 ,但“ontouchstart”事件在$窗口不办理,我想保持启用提示药片。

Is there any way I can get a workaround this? 有什么办法可以解决这个问题吗?

I finally decided to inject a small css modification in run time. 我最终决定在运行时注入一个小的CSS修改。 I had the three following options: 我有以下三种选择:

1. Use that hack on GitHub : I did not like the fact that in order to make it work I had to empirically (by placing a breaking point) find out which were the actual services injected in the $get function of the uibTooltipProvider. 1. 在GitHub上使用该hack :我不喜欢这样一个事实,为了使其能够正常工作,我必须凭经验(通过设置一个断点)来找出注入uibTooltipProvider的$ get函数中的实际服务是什么。 (see there is a difference between the services injected in the github thread and the ones I had to inject (see code snippet) (请参阅在github线程中注入的服务与我必须注入的服务之间的区别(请参见代码段)

2. Add the bowser library and use it statically: I did not like this option because we are already using angular-bowser as a dependency for our DeviceDetector service, so we would be using the same library twice: one statically to configure the tooltip options and one in runtime for everything else. 2.添加Bowser库并静态使用它:我不喜欢该选项,因为我们已经将angular-bowser用作DeviceDetector服务的依赖项,因此我们将使用相同的库两次:一个静态地配置工具提示选项在运行时进行其他所有操作。

3. Inject a small css modification (the option I chose): 3.注入一个小的CSS修改(我选择的选项):

public disableTooltipsForTouchScreen(): void { if ( this._deviceDetector.isMobile() || this._deviceDetector.isTablet() ) { let styleSheet = document.createElement("style"); styleSheet["innerHTML"] = ".tooltip { display: none; }"; document.body.appendChild(styleSheet); } }

And if we ever need to have a finest control over the bootstrap-tooltip configuration then I will consider option 2. 如果我们需要对bootstrap-tooltip配置进行最好的控制,那么我将考虑选项2。

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

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