2016-10-19 16:41:04 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-10-19 16:41:04 -04:00
|
|
|
*
|
|
|
|
* 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 09:20:28 -05:00
|
|
|
|
2017-09-25 07:44:24 -04:00
|
|
|
import {NgZone, PlatformRef, Type} from '@angular/core';
|
2016-10-20 22:35:35 -04:00
|
|
|
import {UpgradeModule} from '@angular/upgrade/static';
|
2019-03-22 05:42:52 -04:00
|
|
|
import * as angular from '../../../src/common/src/angular1';
|
|
|
|
import {$EXCEPTION_HANDLER, $ROOT_SCOPE} from '../../../src/common/src/constants';
|
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 09:20:28 -05:00
|
|
|
|
2016-10-19 16:41:04 -04:00
|
|
|
export function bootstrap(
|
|
|
|
platform: PlatformRef, Ng2Module: Type<{}>, element: Element, ng1Module: angular.IModule) {
|
2017-07-19 17:52:49 -04:00
|
|
|
// We bootstrap the Angular module first; then when it is ready (async) we bootstrap the AngularJS
|
|
|
|
// module on the bootstrap element (also ensuring that AngularJS errors will fail the test).
|
2016-10-19 16:41:04 -04:00
|
|
|
return platform.bootstrapModule(Ng2Module).then(ref => {
|
2017-09-25 07:44:24 -04:00
|
|
|
const ngZone = ref.injector.get<NgZone>(NgZone);
|
2017-07-19 17:52:49 -04:00
|
|
|
const upgrade = ref.injector.get(UpgradeModule);
|
|
|
|
const failHardModule: any = ($provide: angular.IProvideService) => {
|
2020-04-13 19:40:21 -04:00
|
|
|
$provide.value($EXCEPTION_HANDLER, (err: any) => {
|
|
|
|
throw err;
|
|
|
|
});
|
2017-07-19 17:52:49 -04:00
|
|
|
};
|
2017-09-25 07:44:24 -04:00
|
|
|
|
|
|
|
// The `bootstrap()` helper is used for convenience in tests, so that we don't have to inject
|
|
|
|
// and call `upgrade.bootstrap()` on every Angular module.
|
|
|
|
// In order to closer emulate what happens in real application, ensure AngularJS is bootstrapped
|
|
|
|
// inside the Angular zone.
|
|
|
|
//
|
|
|
|
ngZone.run(() => upgrade.bootstrap(element, [failHardModule, ng1Module.name]));
|
|
|
|
|
2016-10-19 16:41:04 -04:00
|
|
|
return upgrade;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-04 10:19:09 -05:00
|
|
|
export function $apply(adapter: UpgradeModule, exp: angular.Ng1Expression) {
|
|
|
|
const $rootScope = adapter.$injector.get($ROOT_SCOPE) as angular.IRootScopeService;
|
|
|
|
$rootScope.$apply(exp);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function $digest(adapter: UpgradeModule) {
|
2016-10-20 06:47:56 -04:00
|
|
|
const $rootScope = adapter.$injector.get($ROOT_SCOPE) as angular.IRootScopeService;
|
|
|
|
$rootScope.$digest();
|
|
|
|
}
|