Experimental Class
Part of the upgrade/static library for hybrid upgrade apps that support AoT compilation
Allows Angular 1 and Angular 2+ components to be used together inside a hybrid upgrade application, which supports AoT compilation.
Specifically, the classes and functions in the upgrade/static module allow the following:
UpgradeComponent.downgradeComponent.downgradeInjectable.When reasoning about how a hybrid application works it is useful to have a mental model which describes what is happening and explains what is happening at the lowest level.
UpgradeComponent.downgradeComponent.[...]) for property binding.$apply().import {UpgradeModule} from '@angular/upgrade/static';
Import the UpgradeModule into your top level Angular 2+ NgModule.
// This NgModule represents the Angular 2 pieces of the application
@NgModule({
declarations: [Ng2HeroesComponent, Ng1HeroComponentWrapper],
providers: [
HeroesService,
// Register an Angular 2+ provider whose value is the "upgraded" Angular 1 service
{provide: 'titleCase', useFactory: (i: any) => i.get('titleCase'), deps: ['$injector']}
],
// All components that are to be "downgraded" must be declared as `entryComponents`
entryComponents: [Ng2HeroesComponent],
// We must import `UpgradeModule` to get access to the Angular 1 core services
imports: [BrowserModule, UpgradeModule]
})
class Ng2AppModule {
ngDoBootstrap() { /* this is a placeholder to stop the boostrapper from complaining */
}
}
Then bootstrap the hybrid upgrade app's module, get hold of the UpgradeModule instance and use it to bootstrap the top level Angular 1 module.
// First we bootstrap the Angular 2 HybridModule
// (We are using the dynamic browser platform as this example has not been compiled AoT)
platformBrowserDynamic().bootstrapModule(Ng2AppModule).then(ref => {
// Once Angular 2 bootstrap is complete then we bootstrap the Angular 1 module
const upgrade = ref.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.body, [ng1AppModule.name]);
});
There is no specific API for upgrading an Angular 1 service. Instead you should just follow the following recipe:
Let's say you have an Angular 1 service:
// This Angular 1 service will be "upgraded" to be used in Angular 2+
ng1AppModule.factory(
'titleCase',
() => (value: string) => value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase()));
Then you should define an Angular 2+ provider to be included in your NgModule providers property.
// Register an Angular 2+ provider whose value is the "upgraded" Angular 1 service
{provide: 'titleCase', useFactory: (i: any) => i.get('titleCase'), deps: ['$injector']}
Then you can use the "upgraded" Angular 1 service by injecting it into an Angular 2 component or service.
constructor(@Inject('titleCase') titleCase: (v: string) => string) {
// Change all the hero names to title case, using the "upgraded" Angular 1 service
this.heroes.forEach((hero: Hero) => hero.name = titleCase(hero.name));
}
class UpgradeModule {
constructor(injector: Injector, ngZone: NgZone)
$injector : any
injector : Injector
ngZone : NgZone
bootstrap(element: Element, modules?: string[], config?: any)
}
This class is an NgModule, which you import to provide Angular 1 core services, and has an instance method used to bootstrap the hybrid upgrade application.
Importing this NgModule will add providers for the core Angular 1 services to the root injector.
The runtime instance of this class contains a bootstrap() method, which you use to bootstrap the top level Angular 1 module onto an element in the DOM for the hybrid upgrade app.
It also contains properties to access the root injector, the bootstrap NgZone and the Angular 1 $injector.
@NgModule({providers: angular1Providers}) constructor(injector: Injector, ngZone: NgZone)
$injector : any
The Angular 1 $injector for the upgrade application.
injector : Injector
The root Injector for the upgrade application.
ngZone : NgZone
The bootstrap zone for the upgrade application
bootstrap(element: Element, modules?: string[], config?: any)
Bootstrap an Angular 1 application from this NgModule
exported from @angular/upgrade/static, defined in @angular/upgrade/src/aot/upgrade_module.ts
© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v2.angular.io/docs/ts/latest/api/upgrade/static/UpgradeModule-class.html