加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS有两个不同的$injectors

发布时间:2020-12-17 07:12:52 所属栏目:安全 来源:网络整理
导读:今天我发现,注入配置或提供商的$injector不同于注入服务,工厂或控制器的$injector. 而且这个$inject的get()函数的工作方式不同. 来自配置或提供商的$injector,无法获取()任何服务! $injector.get(‘myService’)抛出错误:[$injector:unpr]未知提供者:myS
今天我发现,注入配置或提供商的$injector不同于注入服务,工厂或控制器的$injector.

而且这个$inject的get()函数的工作方式不同.

来自配置或提供商的$injector,无法获取()任何服务! $injector.get(‘myService’)抛出错误:[$injector:unpr]未知提供者:myService,但$injector.has(‘myService’)返回true.这非常奇怪.

来自服务或控制器的$injector工作正常.

以下是一个代码示例,以便更好地理解:

angular.module('app',[])

        .provider('myProvider',function ($injector) {
            this.$get = ['$injector',function (serviceInjector) {
                return {
                    providerInjector: $injector,serviceInjector: serviceInjector
                };
            }];
        })

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

        .controller('myCtrl',function ($scope,myProvider) {
            var providerInjector = myProvider.providerInjector;
            var serviceInjector = myProvider.serviceInjector;

            console.log(providerInjector === serviceInjector); // -> false

            console.log(serviceInjector.has('myService')); // `serviceInjector` has `myService`
            console.log(getMyService(serviceInjector)); // `serviceInjector` can get `myService`

            console.log(providerInjector.has('myService')); // `providerInjector` has `myService` too!
            console.log(getMyService(providerInjector)); // but `providerInjector` can't get `myService`! =(

            function getMyService(injector) {
                try {
                    injector.get('myService');
                    return "OK";
                } catch (e) {
                    return e.toString();
                }
            }

        });

Here is a plunker to play

谁能解释为什么有两种不同的注射器?

如何从provider / config使用$injector注入服务(当然,在服务初始化之后)?

附:我使用角1.3.13

解决方法

我在github上找到了这个问题: https://github.com/angular/angular.js/issues/5559

In the config function,$injector is the provider injector,where in the run function,$injector is the instance injector.

One’s the $injector at the config stage (only providers and constants accessible),and one’s the $injector at the run stage. The confusion may be that you’re thinking the $injector modifies itself to include the new stuff as it crosses the line from config to run,but that’s not true. They’re two separate (although related) objects,with their own caches of instances.

A more in-depth reason for this dichotomy will probably come from a deep learning of the $injector internals,but it seems like it’s been DRY-ed pretty hardcore,and the two types of injectors share almost all the same behavior,except in how they deal with “cache misses” in their instance caches.

We are going to overhaul the injector in v2,so this will get fixed there (getting rid of the config phase is one of the objectives of the injector v2).

看起来确实有两种不同的注入器,角度开发人员不会修复这种行为(在版本< 2.0中).由于某种原因,没有人在$injector文档中添加关于该方面的注释. 我无法找到一种方法如何在没有hacky技巧的情况下在配置块中真正获得实例注入器.所以,我写了一个可爱的提供者来解决这类问题.

.provider('instanceInjector',function () {

    var instanceInjector;

    function get() {
        return instanceInjector;
    }

    function exists() {
        return !!instanceInjector;
    }

    angular.extend(this,{
        get: get,exists: exists
    });

    this.$get = function ($injector) {
        instanceInjector = $injector;

        return {
            get: get,exists: exists
        };
    }
})

// We need to inject service somewhere.
// Otherwise $get function will be never executed
.run(['instanceInjector',function(instanceInjector){}])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读