today found, $injector
injected config or provider different $injector
injected service, factory or controller.
and get()
function $injectors works differently.
$injector
config or provider, can't get()
service! $injector.get('myservice')
throws error: [$injector:unpr] unknown provider: myservice
, $injector.has('myservice')
return true. that's very strange.
$injector
service or controller works normally.
here code sample better understanding:
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 `myservice` console.log(providerinjector.has('myservice')); // `providerinjector` has `myservice` too! console.log(getmyservice(providerinjector)); // `providerinjector` can't `myservice`! =( function getmyservice(injector) { try { injector.get('myservice'); return "ok"; } catch (e) { return e.tostring(); } } });
can explain why there 2 different injectors?
and how can use $injector provider/config inject service(after service initialized, of course)?
p.s. use angular 1.3.13
i found issue on github: https://github.com/angular/angular.js/issues/5559
in config function, $injector provider injector, in run function, $injector instance injector.
one's $injector @ config stage (only providers , constants accessible), , one's $injector @ run stage. confusion may you're thinking $injector modifies include new stuff crosses line config run, that's not true. they're 2 separate (although related) objects, own caches of instances.
a more in-depth reason dichotomy come deep learning of $injector internals, seems it's been dry-ed pretty hardcore, , 2 types of injectors share same behavior, except in how deal "cache misses" in instance caches.
we going overhaul injector in v2, fixed there (getting rid of config phase 1 of objectives of injector v2).
seems there 2 different injectors, , angular developers not fix behavior(in versions <2.0). , nobody added note aspect $injector docs reason.
i unable find way how instance injector inside configuration block without hacky tricks. so, write cute provider solve kind of problems.
.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 }; } }) // need inject service somewhere. // otherwise $get function never executed .run(['instanceinjector', function(instanceinjector){}])