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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {Injector} from '@angular/core';
|
|
|
|
|
import {INJECTOR_KEY} from './constants';
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-16 10:17:10 +00:00
|
|
|
* @whatItDoes
|
2016-10-19 21:41:04 +01:00
|
|
|
*
|
2017-05-09 23:51:37 +01:00
|
|
|
* *Part of the [upgrade/static](api?query=upgrade%2Fstatic)
|
2016-11-16 10:17:10 +00:00
|
|
|
* library for hybrid upgrade apps that support AoT compilation*
|
2016-10-19 21:41:04 +01:00
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* Allow an Angular service to be accessible from AngularJS.
|
2016-11-16 10:17:10 +00:00
|
|
|
*
|
|
|
|
|
* @howToUse
|
|
|
|
|
*
|
|
|
|
|
* First ensure that the service to be downgraded is provided in an {@link NgModule}
|
|
|
|
|
* that will be part of the upgrade application. For example, let's assume we have
|
|
|
|
|
* defined `HeroesService`
|
|
|
|
|
*
|
|
|
|
|
* {@example upgrade/static/ts/module.ts region="ng2-heroes-service"}
|
|
|
|
|
*
|
|
|
|
|
* and that we have included this in our upgrade app {@link NgModule}
|
|
|
|
|
*
|
|
|
|
|
* {@example upgrade/static/ts/module.ts region="ng2-module"}
|
|
|
|
|
*
|
|
|
|
|
* Now we can register the `downgradeInjectable` factory function for the service
|
2017-01-26 22:30:42 -08:00
|
|
|
* on an AngularJS module.
|
2016-11-16 10:17:10 +00:00
|
|
|
*
|
|
|
|
|
* {@example upgrade/static/ts/module.ts region="downgrade-ng2-heroes-service"}
|
|
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* Inside an AngularJS component's controller we can get hold of the
|
2016-11-16 10:17:10 +00:00
|
|
|
* downgraded service via the name we gave when downgrading.
|
|
|
|
|
*
|
|
|
|
|
* {@example upgrade/static/ts/module.ts region="example-app"}
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* Takes a `token` that identifies a service provided from Angular.
|
2016-11-16 10:17:10 +00:00
|
|
|
*
|
|
|
|
|
* Returns a [factory function](https://docs.angularjs.org/guide/di) that can be
|
2017-01-26 22:30:42 -08:00
|
|
|
* used to register the service on an AngularJS module.
|
2016-11-16 10:17:10 +00:00
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* The factory function provides access to the Angular service that
|
2016-11-16 10:17:10 +00:00
|
|
|
* is identified by the `token` parameter.
|
2016-10-19 21:41:04 +01:00
|
|
|
*
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
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
|
|
|
export function downgradeInjectable(token: any): Function {
|
|
|
|
|
const factory = function(i: Injector) { return i.get(token); };
|
2017-05-12 10:03:38 -07:00
|
|
|
(factory as any)['$inject'] = [INJECTOR_KEY];
|
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
|
|
|
|
|
|
|
|
return factory;
|
|
|
|
|
}
|