AngularJS UI 路由器在工厂/服务中使用已解析的依赖项 [英] AngularJS UI Router using resolved dependency in factory / service

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

问题描述

我有一个 UI 路由器定义如下(为简单起见进行了修剪):

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'
        });

someState 状态正在使用依赖于该 model 解析的工厂/服务.它的定义是这样的,AngularJS 在这里抛出一个 Unknown provider: modelProvider <- model <- someService 错误:

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) { ... }

但是,在此状态的控制器中使用相同的 model 解析工作正常:

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

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

所以我理解 UI 路由器会延迟 SomeController 的 DI,直到解析发生,这使得 AngularJS 不会抛出错误.但是,当将该解析作为依赖于 someService 时,为什么不会发生相同的延迟?解析只适用于控制器吗?如果是这种情况,我如何在工厂/服务中使用解析?

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.

换句话说:

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

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

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