<small id='kmLtH'></small><noframes id='kmLtH'>

    <bdo id='kmLtH'></bdo><ul id='kmLtH'></ul>

        <tfoot id='kmLtH'></tfoot>
        <i id='kmLtH'><tr id='kmLtH'><dt id='kmLtH'><q id='kmLtH'><span id='kmLtH'><b id='kmLtH'><form id='kmLtH'><ins id='kmLtH'></ins><ul id='kmLtH'></ul><sub id='kmLtH'></sub></form><legend id='kmLtH'></legend><bdo id='kmLtH'><pre id='kmLtH'><center id='kmLtH'></center></pre></bdo></b><th id='kmLtH'></th></span></q></dt></tr></i><div id='kmLtH'><tfoot id='kmLtH'></tfoot><dl id='kmLtH'><fieldset id='kmLtH'></fieldset></dl></div>
      1. <legend id='kmLtH'><style id='kmLtH'><dir id='kmLtH'><q id='kmLtH'></q></dir></style></legend>
      2. AngularJS UI路由器在工厂/服务中使用已解析的依赖项

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

          <small id='lxDik'></small><noframes id='lxDik'>

            <legend id='lxDik'><style id='lxDik'><dir id='lxDik'><q id='lxDik'></q></dir></style></legend>
              <tbody id='lxDik'></tbody>

            <i id='lxDik'><tr id='lxDik'><dt id='lxDik'><q id='lxDik'><span id='lxDik'><b id='lxDik'><form id='lxDik'><ins id='lxDik'></ins><ul id='lxDik'></ul><sub id='lxDik'></sub></form><legend id='lxDik'></legend><bdo id='lxDik'><pre id='lxDik'><center id='lxDik'></center></pre></bdo></b><th id='lxDik'></th></span></q></dt></tr></i><div id='lxDik'><tfoot id='lxDik'></tfoot><dl id='lxDik'><fieldset id='lxDik'></fieldset></dl></div>
              • <bdo id='lxDik'></bdo><ul id='lxDik'></ul>

                1. <tfoot id='lxDik'></tfoot>
                  本文介绍了AngularJS 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'
                          });
                  

                  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路由器在工厂/服务中使用已解析的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                      • <bdo id='rBBvf'></bdo><ul id='rBBvf'></ul>

                        <legend id='rBBvf'><style id='rBBvf'><dir id='rBBvf'><q id='rBBvf'></q></dir></style></legend>
                          <tbody id='rBBvf'></tbody>
                        <tfoot id='rBBvf'></tfoot>

                        <small id='rBBvf'></small><noframes id='rBBvf'>

                        <i id='rBBvf'><tr id='rBBvf'><dt id='rBBvf'><q id='rBBvf'><span id='rBBvf'><b id='rBBvf'><form id='rBBvf'><ins id='rBBvf'></ins><ul id='rBBvf'></ul><sub id='rBBvf'></sub></form><legend id='rBBvf'></legend><bdo id='rBBvf'><pre id='rBBvf'><center id='rBBvf'></center></pre></bdo></b><th id='rBBvf'></th></span></q></dt></tr></i><div id='rBBvf'><tfoot id='rBBvf'></tfoot><dl id='rBBvf'><fieldset id='rBBvf'></fieldset></dl></div>