2016-10-19 21:41:04 +01:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
import * as angular from '../common/angular1';
|
2016-10-19 21:41:04 +01:00
|
|
|
|
|
|
|
// We have to do a little dance to get the ng1 injector into the module injector.
|
|
|
|
// We store the ng1 injector so that the provider in the module injector can access it
|
|
|
|
// Then we "get" the ng1 injector from the module injector, which triggers the provider to read
|
|
|
|
// the stored injector and release the reference to it.
|
2017-03-24 09:59:18 -07:00
|
|
|
let tempInjectorRef: angular.IInjectorService|null;
|
2016-10-19 21:41:04 +01:00
|
|
|
export function setTempInjectorRef(injector: angular.IInjectorService) {
|
|
|
|
tempInjectorRef = injector;
|
|
|
|
}
|
|
|
|
export function injectorFactory() {
|
2017-03-24 09:59:18 -07:00
|
|
|
const injector: angular.IInjectorService|null = tempInjectorRef;
|
2016-10-19 21:41:04 +01:00
|
|
|
tempInjectorRef = null; // clear the value to prevent memory leaks
|
|
|
|
return injector;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rootScopeFactory(i: angular.IInjectorService) {
|
|
|
|
return i.get('$rootScope');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function compileFactory(i: angular.IInjectorService) {
|
|
|
|
return i.get('$compile');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseFactory(i: angular.IInjectorService) {
|
|
|
|
return i.get('$parse');
|
|
|
|
}
|
|
|
|
|
|
|
|
export const angular1Providers = [
|
|
|
|
// We must use exported named functions for the ng2 factories to keep the compiler happy:
|
|
|
|
// > Metadata collected contains an error that will be reported at runtime:
|
|
|
|
// > Function calls are not supported.
|
|
|
|
// > Consider replacing the function or lambda with a reference to an exported function
|
|
|
|
{provide: '$injector', useFactory: injectorFactory},
|
|
|
|
{provide: '$rootScope', useFactory: rootScopeFactory, deps: ['$injector']},
|
|
|
|
{provide: '$compile', useFactory: compileFactory, deps: ['$injector']},
|
|
|
|
{provide: '$parse', useFactory: parseFactory, deps: ['$injector']}
|
|
|
|
];
|