填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

UpgradeModule

An NgModule, which you import to provide AngularJS core services, and has an instance method used to bootstrap the hybrid upgrade application.

查看"说明"...

      
      class UpgradeModule {
  $injector: any
  injector: Injector
  ngZone: NgZone
  bootstrap(element: Element, modules: string[] = [], config?: any)
}
    

说明

Part of the upgrade/static library for hybrid upgrade apps that support AOT compilation

The upgrade/static package contains helpers that allow AngularJS and Angular 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:

  1. Creation of an Angular directive that wraps and exposes an AngularJS component so that it can be used in an Angular template. See UpgradeComponent.
  2. Creation of an AngularJS directive that wraps and exposes an Angular component so that it can be used in an AngularJS template. See downgradeComponent.
  3. Creation of an Angular root injector provider that wraps and exposes an AngularJS service so that it can be injected into an Angular context. See Upgrading an AngularJS service below.
  4. Creation of an AngularJS service that wraps and exposes an Angular injectable so that it can be injected into an AngularJS context. See downgradeInjectable.
  5. Bootstrapping of a hybrid Angular application which contains both of the frameworks coexisting in a single application.

属性

属性说明
$injector: any

The AngularJS $injector for the upgrade application.

injector: Injector

The Angular Injector *

ngZone: NgZone

The bootstrap zone for the upgrade application

方法

Bootstrap an AngularJS application from this NgModule

bootstrap(element: Element, modules: string[] = [], config?: any)
      
      bootstrap(element: Element, modules: string[] = [], config?: any)
    
参数
element Element

the element on which to bootstrap the AngularJS application

modules string[]

可选. 默认值是 [].

the AngularJS modules to bootstrap for this application

config any

可选. 默认值是 undefined.

optional extra AngularJS bootstrap configuration

提供商

提供商
angular1Providers
      
      angular1Providers
    

使用说明

import {UpgradeModule} from '@angular/upgrade/static';
      
      import {UpgradeModule} from '@angular/upgrade/static';
    

See also the examples below.

Mental Model

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.

  1. There are two independent frameworks running in a single application, each framework treats the other as a black box.
  2. Each DOM element on the page is owned exactly by one framework. Whichever framework instantiated the element is the owner. Each framework only updates/interacts with its own DOM elements and ignores others.
  3. AngularJS directives always execute inside the AngularJS framework codebase regardless of where they are instantiated.
  4. Angular components always execute inside the Angular framework codebase regardless of where they are instantiated.
  5. An AngularJS component can be "upgraded"" to an Angular component. This is achieved by defining an Angular directive, which bootstraps the AngularJS component at its location in the DOM. See UpgradeComponent.
  6. An Angular component can be "downgraded" to an AngularJS component. This is achieved by defining an AngularJS directive, which bootstraps the Angular component at its location in the DOM. See downgradeComponent.
  7. Whenever an "upgraded"/"downgraded" component is instantiated the host element is owned by the framework doing the instantiation. The other framework then instantiates and owns the view for that component.

    1. This implies that the component bindings will always follow the semantics of the instantiation framework.
    2. The DOM attributes are parsed by the framework that owns the current template. So attributes in AngularJS templates must use kebab-case, while AngularJS templates must use camelCase.
    3. However the template binding syntax will always use the Angular style, e.g. square brackets ([...]) for property binding.
  8. Angular is bootstrapped first; AngularJS is bootstrapped second. AngularJS always owns the root component of the application.
  9. The new application is running in an Angular zone, and therefore it no longer needs calls to $apply().

The UpgradeModule class

This class is an NgModule, which you import to provide AngularJS core services, and has an instance method used to bootstrap the hybrid upgrade application.

  • Core AngularJS services Importing this NgModule will add providers for the core AngularJS services to the root injector.

  • Bootstrap The runtime instance of this class contains a `bootstrap()` method, which you use to bootstrap the top level AngularJS 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 AngularJS $injector.

Examples

Import the UpgradeModule into your top level Angular `NgModule`.

// This NgModule represents the Angular pieces of the application @NgModule({ declarations: [Ng2HeroesComponent, Ng1HeroComponentWrapper], providers: [ HeroesService, // Register an Angular provider whose value is the "upgraded" AngularJS service {provide: TextFormatter, useFactory: (i: any) => i.get('textFormatter'), 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 AngularJS core services imports: [BrowserModule, UpgradeModule] }) export class Ng2AppModule { }
      
      // This NgModule represents the Angular pieces of the application
@NgModule({
  declarations: [Ng2HeroesComponent, Ng1HeroComponentWrapper],
  providers: [
    HeroesService,
    // Register an Angular provider whose value is the "upgraded" AngularJS service
    {provide: TextFormatter, useFactory: (i: any) => i.get('textFormatter'), 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 AngularJS core services
  imports: [BrowserModule, UpgradeModule]
})
export class Ng2AppModule {
}
    

Then inject UpgradeModule into your Angular NgModule and use it to bootstrap the top level AngularJS module in the ngDoBootstrap() method.

export class Ng2AppModule { constructor(private upgrade: UpgradeModule) {} ngDoBootstrap() { // We bootstrap the AngularJS app. this.upgrade.bootstrap(document.body, [ng1AppModule.name]); } }
      
      export class Ng2AppModule {
  constructor(private upgrade: UpgradeModule) {}

  ngDoBootstrap() {
    // We bootstrap the AngularJS app.
    this.upgrade.bootstrap(document.body, [ng1AppModule.name]);
  }
}
    

Finally, kick off the whole process, by bootstraping your top level Angular NgModule.

// We bootstrap the Angular module as we would do in a normal Angular app. // (We are using the dynamic browser platform as this example has not been compiled AOT.) platformBrowserDynamic().bootstrapModule(Ng2AppModule);
      
      // We bootstrap the Angular module as we would do in a normal Angular app.
// (We are using the dynamic browser platform as this example has not been compiled AOT.)
platformBrowserDynamic().bootstrapModule(Ng2AppModule);
    

Upgrading an AngularJS service

There is no specific API for upgrading an AngularJS service. Instead you should just follow the following recipe:

Let's say you have an AngularJS service:

export class TextFormatter { titleCase(value: string) { return value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase()); } } // This AngularJS service will be "upgraded" to be used in Angular ng1AppModule.service('textFormatter', [TextFormatter]);
      
      export class TextFormatter {
  titleCase(value: string) { return value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase()); }
}

// This AngularJS service will be "upgraded" to be used in Angular
ng1AppModule.service('textFormatter', [TextFormatter]);
    

Then you should define an Angular provider to be included in your NgModule providers property.

// Register an Angular provider whose value is the "upgraded" AngularJS service {provide: TextFormatter, useFactory: (i: any) => i.get('textFormatter'), deps: ['$injector']}
      
      // Register an Angular provider whose value is the "upgraded" AngularJS service
{provide: TextFormatter, useFactory: (i: any) => i.get('textFormatter'), deps: ['$injector']}
    

Then you can use the "upgraded" AngularJS service by injecting it into an Angular component or service.

constructor(textFormatter: TextFormatter) { // Change all the hero names to title case, using the "upgraded" AngularJS service this.heroes.forEach((hero: Hero) => hero.name = textFormatter.titleCase(hero.name)); }
      
      constructor(textFormatter: TextFormatter) {
  // Change all the hero names to title case, using the "upgraded" AngularJS service
  this.heroes.forEach((hero: Hero) => hero.name = textFormatter.titleCase(hero.name));
}