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

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

问题描述

我想将一些重复性的逻辑出一个可重用的服务,它的必须在配置阶段可的。并可能有机会获得 $ xxxProvider 依赖注射了。这样的事情可能吗?一个人能与工厂/供应商/服务做到这一点/恒?

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路由器,我注意到,对于CRUD的路线都是一样的,除了资源的名称。所以,我想移动某个配置CRUD路线,使路由定义更干的这个逻辑的。

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'.

待办事项,该服务注册需要拿出的配置,如果他们居住在同一个文件块之前。要克服这一点的方法,将提取您服务到一个单独的模块,并注入它作为一个依赖于你的主模块。事情是这样的:

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. 注入另一个提供商到自己的供应商,通过功能传递从一个构造函数到另一个。

  3. 注册一个次要的服务,这被注入提供商构造。

  4. 传递从的app.config 块多一些功能,你的供应商的构造。

  5. 注入由构造返回到控制器的实例。

  6. 输出从实例最终数据由构造返回,与ARGS。

  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,从提供商

  • 容易发生DI,从服务。

  • 在配置阶段可用。

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

在这里是为提供的文档 速记方法,以及它们是如何不同。正如你可以清楚地看到,在提供商核心功能是一个与向它提供最多的选择 - 但也是最难的工作,原因很明显。

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的code,使其更容易理解只是这一切是如何联系在了一起。

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天全站免登陆