简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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