利用工厂/服务解决依赖AngularJS UI路由器 [英] AngularJS UI Router using resolved dependency in factory / service

查看:147
本文介绍了利用工厂/服务解决依赖AngularJS UI路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UI定义路由器这样的事情(修剪为简单起见):

  $ stateProvider
        .STATE('someState',{
            解析:{
                型号:['modelService','信息',函数(modelService,资讯){
                    返回modelService.get(info.id)$承诺。
                }]
            },
            控制器:'SomeController
        });

someState 状态使用工厂/服务,是依赖于模式的决心。它的定义是这样的,和AngularJS抛出的未知提供商:modelProvider< - 模型与LT; - someService 错误的位置:

 
    .module('someModule')
    .factory('someService',someService);。someService $注射= ['模式'];
功能someService(模型){...}

然而,使用相同的模式解决这个国家的控制器内的正常工作

  SomeController $注射= ['模式'];
功能SomeController(模型){...}

所以我的理解是UI路由器延迟的DI的 SomeController ,直到解决正在发生的事情,这使得AngularJS不抛出异常。但是,怎么会相同的延迟是不是把这种决心的时候作为一个依赖于 someService 怎么回事?不要做出决议只在控制器工作?如果是这样的话,我怎么能使用下决心工厂/服务中?


解决方案

  

待办事项解决控制器上唯一的工作?


是的,在解析只有控制器工作。


  

如果是这样的话,我怎么可以用一个解决工厂/服务中?


请记住,工厂和服务回报的的对象,即第一次工厂注入到控制器,它运行的任何实例code你提供,并创建一个对象,然后任何后续倍,工厂被实例化,返回相同的对象。

在换句话说:

  angular.module('someModule')
.factory('SomeFactory',函数(){
  //这个code只运行一次
  对象= {}
  object.now = Date.now();
  返回对象
);

SomeFactory.now 将当前时间在第一时间工厂注入到控制器,但它的不可以在后续使用更新。

因此​​,决心为工厂的概念并没有真正意义。如果你想有,做动态的东西(这显然是很常见的)一个服务,你需要把里面的逻辑功能的单例。

例如,你给了code样品中,你的工厂依赖于一种模式。一种方法是注入模型转换成使用你已经有了设立的决心方法控制器,然后露出接受一个模型,做你需要做什么,像这样单身的方法:

  angular.module('someModule')
.factory('SomeFactory',函数(){
  返回{
    doSomethingWithModel:函数(模型){
      $ http.post('地方',模型);
  }
});
.controller('SomeController',函数(SomeFactory,型号){
  SomeFactory.doSomethingWithModel(模型);
});

另外,如果你没有在控制器中需要解决的价值可言,不要把它直接在决心,而不是把决心变成逻辑的方法对服务的单,并调用里面解决这个方法,传递结果给控制器。

这很难与抽象的交谈更细致,因此,如果您需要进一步的指针然后提供具体的使用情况。

I have a UI Router defined something like this (trimmed for simplicity):

    $stateProvider
        .state('someState', {
            resolve: {
                model: ['modelService', 'info', function (modelService, info) {
                    return modelService.get(info.id).$promise;
                }]
            },
            controller: 'SomeController'
        });

This someState state is using a factory / service that is dependent on that model resolve. It's defined something like this, and AngularJS throws an Unknown provider: modelProvider <- model <- someService error here:

angular
    .module('someModule')
    .factory('someService', someService);

someService.$inject = ['model'];
function someService(model) { ... }

However, using the same model resolve inside of this state's controller works fine:

SomeController.$inject = ['model'];
function SomeController(model) { ... }

So I'm understanding that UI Router is delaying the DI of the SomeController until the resolve is happening, which allows AngularJS to not throw an error. However, how come the same delay is not happening when putting that resolve as a dependency on someService? Do resolves only work on controllers? And if that is the case, how can I use a resolve inside a factory / service?

解决方案

Do resolves only work on controllers?

Yes, resolves only work on controllers.

And if that is the case, how can I use a resolve inside a factory / service?

Remember that factories and services return singleton objects, i.e. the first time a factory is injected into a controller, it runs any instantiation code you provide and creates an object, and then any subsequent times that factory is instantiated, that same object is returned.

In other words:

angular.module('someModule')
.factory( 'SomeFactory' , function () {
  // this code only runs once
  object = {}
  object.now = Date.now();
  return object
);

SomeFactory.now will be the current time the first time the factory is injected into a controller, but it not update on subsequent usage.

As such, the concept of resolve for a factory doesn't really make sense. If you want to have a service that does something dynamically (which is obviously very common), you need to put the logic inside functions on the singleton.

For example, in the code sample you gave, your factory depended on a model. One approach would be to inject the model into the controller using the resolve method you've already got set up, then expose a method on the singleton that accepts a model and does what you need to do, like so:

angular.module('someModule')
.factory( 'SomeFactory', function () {
  return {
    doSomethingWithModel: function (model) {
      $http.post('wherever', model);
  }
});
.controller('SomeController', function (SomeFactory, model) {
  SomeFactory.doSomethingWithModel(model);
});

Alternatively, if you don't need the resolved value in the controller at all, don't put it directly in a resolve, instead put the resolve logic into a method on the service's singleton and call that method inside the resolve, passing the result to the controller.

It's hard to be more detailed with abstract conversation, so if you need further pointers then provide a specific use-case.

这篇关于利用工厂/服务解决依赖AngularJS UI路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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