Class Overview
class UpgradeAdapter {
constructor
(ng2AppModule: Type<any>, compilerOptions?: CompilerOptions)
downgradeNg2Component
(type: Type<any>) : Function
upgradeNg1Component
(name: string) : Type<any>
registerForNg1Tests
(modules?: string[]) : UpgradeAdapterRef
bootstrap
(element: Element, modules?: any[], config?:IAngularBootstrapConfig) : UpgradeAdapterRef
upgradeNg1Provider
(name: string, options?: {asToken: any})
downgradeNg2Provider
(token: any) : Function
}
Class Description
Use UpgradeAdapter
to allow Angular 1 and Angular 2+ to coexist in a single application.
The UpgradeAdapter
allows:
- creation of Angular 2+ component from Angular 1 component directive (See [UpgradeAdapter#upgradeNg1Component()])
- creation of Angular 1 directive from Angular 2+ component. (See [UpgradeAdapter#downgradeNg2Component()])
- Bootstrapping of a hybrid Angular application which contains both of the frameworks coexisting in a single application.
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.
- There are two independent frameworks running in a single application, each framework treats the other as a black box.
- 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.
- Angular 1 directives always execute inside Angular 1 framework codebase regardless of where they are instantiated.
- Angular 2+ components always execute inside Angular 2+ framework codebase regardless of where they are instantiated.
- An Angular 1 component can be upgraded to an Angular 2+ component. This creates an Angular 2+ directive, which bootstraps the Angular 1 component directive in that location.
- An Angular 2+ component can be downgraded to an Angular 1 component directive. This creates an Angular 1 directive, which bootstraps the Angular 2+ component in that location.
- Whenever an adapter 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. This implies that component bindings will always follow the semantics of the instantiation framework. The syntax is always that of Angular 2+ syntax.
- Angular 1 is always bootstrapped first and owns the bottom most view.
- The new application is running in Angular 2+ zone, and therefore it no longer needs calls to
$apply()
.
Example
Class Details
downgradeNg2Component(type: Type<any>) : Function
Allows Angular 2+ Component to be used from Angular 1.
Use downgradeNg2Component
to create an Angular 1 Directive Definition Factory from
Angular 2+ Component. The adapter will bootstrap Angular 2+ component from within the
Angular 1 template.
Mental Model
- The component is instantiated by being listed in Angular 1 template. This means that the host element is controlled by Angular 1, but the component's view will be controlled by Angular 2+.
- Even thought the component is instantiated in Angular 1, it will be using Angular 2+ syntax. This has to be done, this way because we must follow Angular 2+ components do not declare how the attributes should be interpreted.
Supported Features
- Bindings:
- Attribute:
<comp name="World">
- Interpolation:
<comp greeting="Hello {{name}}!">
- Expression:
<comp [name]="username">
- Event:
<comp (close)="doSomething()">
- Attribute:
- Content projection: yes
upgradeNg1Component(name: string) : Type<any>
Allows Angular 1 Component to be used from Angular 2+.
Use upgradeNg1Component
to create an Angular 2+ component from Angular 1 Component
directive. The adapter will bootstrap Angular 1 component from within the Angular 2+
template.
Mental Model
- The component is instantiated by being listed in Angular 2+ template. This means that the host element is controlled by Angular 2+, but the component's view will be controlled by Angular 1.
Supported Features
- Bindings:
- Attribute:
<comp name="World">
- Interpolation:
<comp greeting="Hello {{name}}!">
- Expression:
<comp [name]="username">
- Event:
<comp (close)="doSomething()">
- Attribute:
- Transclusion: yes
- Only some of the features of
Directive Definition Object are
supported:
compile
: not supported because the host element is owned by Angular 2+, which does not allow modifying DOM structure during compilation.controller
: supported. (NOTE: injection of$attrs
and$transclude
is not supported.)controllerAs
: supported.bindToController
: supported.link
: supported. (NOTE: only pre-link function is supported.)name
: supported.priority
: ignored.replace
: not supported.require
: supported.restrict
: must be set to 'E'.scope
: supported.template
: supported.templateUrl
: supported.terminal
: ignored.transclude
: supported.
registerForNg1Tests(modules?: string[]) : UpgradeAdapterRef
Registers the adapter's Angular 1 upgrade module for unit testing in Angular 1.
Use this instead of angular.mock.module()
to load the upgrade module into
the Angular 1 testing injector.
bootstrap(element: Element, modules?: any[], config?:IAngularBootstrapConfig) : UpgradeAdapterRef
Bootstrap a hybrid Angular 1 / Angular 2+ application.
This bootstrap
method is a direct replacement (takes same arguments) for Angular 1
bootstrap
method. Unlike
Angular 1, this bootstrap is asynchronous.
upgradeNg1Provider(name: string, options?: {asToken: any})
Allows Angular 1 service to be accessible from Angular 2+.
downgradeNg2Provider(token: any) : Function
Allows Angular 2+ service to be accessible from Angular 1.
exported from @angular/upgrade/index, defined in @angular/upgrade/src/upgrade_adapter.ts