简体   繁体   English

为什么我无法使用$ injector.get访问使用$ provide.provider创建的服务

[英]why I can't access a service created using $provide.provider with $injector.get

function configure($provide, $injector) {

    $provide.provider("testservice", function () {
       this.$get = function () {
         this.property = 777;
       };
    });

    var s = $injector.get("testservice");

The last line throws this error: 最后一行抛出此错误:

Unknown provider: testservice

Why so? 为什么这样?

To access provide in config phase, we need to append 'Provider' to the name of the provider. 要在配置阶段访问provide,我们需要在提供程序名称后附加“ Provider”。

module.config(function ($provide, $injector) {
    $provide.provider("testservice", function () {
        this.$get = function () {
            this.property = 777;
        };
    });

    var s = $injector.get("testserviceProvider");
    console.log(s)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM