在配置阶段可用的角度服务 [英] angular service available at configuration phase

查看:25
本文介绍了在配置阶段可用的角度服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些重复的逻辑移出可重用服务,必须在配置阶段可用.并且可能也可以访问 $xxxProvider 依赖项注入.这样的事情可能吗?可以用 factory/provider/service/constant 来做到这一点吗?

I want to move some of the repetitive logic out in a reusable service, which must be available in a config phase. And possibly have access to $xxxProvider dependency injections too. Is such thing possible? Can one do this with factory/provider/service/constant ?

具体来说,当我用 ui-router 定义我的路由时,我注意到 CRUD 的路由都是一样的,除了资源的名称.因此,我想将这种配置 CRUD 路由的逻辑移到某个地方,以使路由定义更加 DRY.

To be specific, when I define my routes with ui-router, I noticed that routes for CRUD are all the same, except for the names of the resources. So i'd like to move this logic of configuring a CRUD route somewhere to make route definitions more DRY.

推荐答案

为了在配置块中访问您的服务提供者,这应该可以正常工作:

For accessing your service provider in the config block, this should work just fine:

app.service('myService', function (/* dependencies */) {
  // service stuff..

  return this; 
});

app.config(function ($provide) {
  $provide.decorator('myService', function ($delegate) {
    // $delegate refers to 'myService'. 
    // You can modify its behaviour and what not in here.

    return $delegate;
  });
});

我不是很清楚你在找什么,但这只是有效".

I'm not crystal clear on what you are looking for, but this 'just works'.

请注意,如果它们驻留在同一文件中,则服务注册需要位于 config 块之前.克服这个问题的一种方法是将您的服务提取到一个单独的模块中,并将其作为对主模块的依赖项注入.像这样:

Do note, that the service registration needs to come before the config block if they reside in the same file. A way to overcome this, would be to extract your service into a separate module and inject it as a dependency to your main module. Something like this:

var serviceModule = angular.module('serviceModule', []);

serviceModule.service('myService', function () {});

var mainModule = angular.module('main', ['serviceModule']);

mainModule.config(function ($provide) {
  $provide.decorator('myService', function ($delegate) {});
});

JsBin:http://jsbin.com/pasiquju/2/edit

我现在整理了一个粗略示例,说明如何执行以下操作:

I've now put together a rough example of how you could do the following:

  1. 注册您自己的自定义提供程序构造函数,该构造函数返回一个对象.
  2. 将另一个 provider 注入您自己的提供程序,将功能从一个构造函数传递到另一个构造函数.
  3. 注册一个辅助服务,它被注入到提供者构造函数中.
  4. 将更多功能从 app.config 块传递给您的提供者构造函数.
  5. 将构造函数返回的实例注入控制器.
  6. 从构造函数返回的实例中输出结束数据,带参数.
  1. Register your own custom provider constructor, that returns an object.
  2. Inject another provider into your own provider, passing through functionality from one constructor to another.
  3. Register a secondary service, that gets injected into the provider constructor.
  4. Pass some more functionality from the app.config block to your provider constructor.
  5. Inject the instance returned by the constructor into a controller.
  6. Output the end data from the instance returned by the constructor, with args.

这应该涵盖指定的需求:

This should cover the needs specified:

  • 易于 DI,来自 providers.
  • 容易从服务中产生 DI.
  • 在配置阶段可用.

JsBin:http://jsbin.com/jenumobi/2/edit

还有 这里是 provider 速记方法的文档,以及它们有何不同.如您所见,provider 核心功能是可用选项最多的功能 - 但也是最难使用的,原因显而易见.

And here's the documentation for the provider shorthand methods, and how they differ. As you can clearly see, the provider core function is the one with the most options available to it - but also the hardest to work with, for obvious reasons.

我希望这对你来说已经足够了,并给你一些关于如何创建自己的 $stateProvider 扩展的想法!我已尽力在 JsBin 中记录代码,以便更容易理解它们是如何联系在一起的.

I hope that will do enough for you, and give you some sort of idea on how to create your own $stateProvider extension! I've done my best to document the code in the JsBin to make it easier to understand just how it all ties together.

这篇关于在配置阶段可用的角度服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆