angular: [2.0.0] UpgradeAdapter - Cannot read property 'injector' of null
I’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Unhandled Promise rejection: TypeError: Cannot read property ‘injector’ of null
Getting above error when trying to inject a downgraded ng2 service into a ng1 service. Except it works when, a) inject into a controller b) in ng1 service use $injector.get(‘myNg2Service’) in a timeout
Error happens here ng1Module.factory(NG2_INJECTOR, () => moduleRef.injector.get(Injector)), which runs before moduleRef gets assigned after _bootstrapModuleWithZone
Expected/desired behavior downgraded ng2 service should be available for injection immediately.
Reproduction of the problem https://plnkr.co/edit/dJRu9g?p=preview
What is the expected behavior? prints out “Hello from ng1 and ng 2 service”
- Angular version: 2.0.0 final
- Browser: all
- Language: Typescript 2.0 & ES5
- SystemJS for ng2
- IIFE for ng1
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 23 (2 by maintainers)
I have the same issue.
@guojenman none of the workarounds worked for me.
Depending on your requirements, you can also use the .ready function of your bootstrap like this:
it is not resolved in angular final release of 2.0.0. Any update on this ?
That seems like a reasonable workaround, but I wouldn’t consider that a fix for this issue. In my opinion, this issue should be reopened, so that it can be fixed within the Angular 2 framework. I don’t think that every consumer of the framework should be forced to workaround this issue.
@Necroskillz I closed this issue because I opened it in regards to UpgradeAdapter, With the release of the UpgradeComponent API (which supports AoT), I doubt much work will be done for UpgradeAdapter.
In regards to using services from and to each frameworks, I’ve found that strategic use of timeouts and manually getting the services from the respective injectors has worked out for me (fairly large app, 60k+ lines of code and 10 different module bundles). I’m sure there will be cases where that may not work. One framework will be to be initialized before the other, so I’m not sure it’s an easy fix.
I would suggest opening a new bug against UpgradeComponent instead if this is important to you.
I had the same issue, but it was a bit trickier to work around and I had to re-structure my service. IMO this is a bug as it could pose a significant issue for upgrading depending on application structure.
Uncaught TypeError: Cannot read property ‘injector’ of null #11632
I kept it in the run() block for now, but I’m manually injecting after a timeout.
Since the services are only created the first time they’re requested from the injector, this delays the creation. My other idea was to put the code on the resolve for my root (abstract) route. Here again, you may need to inject manually.