UpgradeAdapter
Use UpgradeAdapter
to allow AngularJS and Angular to coexist in a single application.
Deprecated: Deprecated since v5. Use upgrade/static
instead, which also supports
Ahead-of-Time compilation.
class UpgradeAdapter {
constructor(ng2AppModule: Type<any>, compilerOptions?: CompilerOptions)
downgradeNg2Component(component: Type<any>): Function
upgradeNg1Component(name: string): Type<any>
registerForNg1Tests(modules?: string[]): UpgradeAdapterRef
bootstrap(element: Element, modules?: any[], config?: angular.IAngularBootstrapConfig): UpgradeAdapterRef
upgradeNg1Provider(name: string, options?: {...})
downgradeNg2Provider(token: any): Function
}
Description
The UpgradeAdapter
allows:
1. creation of Angular component from AngularJS component directive
(See [UpgradeAdapter#upgradeNg1Component()])
2. creation of AngularJS directive from Angular component.
(See [UpgradeAdapter#downgradeNg2Component()])
3. Bootstrapping of a hybrid Angular application which contains both of the frameworks
coexisting in a single application.
Constructor
Parameters
|
Methods
Allows Angular Component to be used from AngularJS. |
||
Parameters
Returns
|
||
Use |
||
Mental Model
Supported Features
Example
|
Allows AngularJS Component to be used from Angular. |
||
Parameters
Returns
|
||
Use |
||
Mental Model
Supported Features
Example
|
Registers the adapter's AngularJS upgrade module for unit testing in AngularJS.
Use this instead of |
||
Parameters
Returns
|
||
Example
|
Bootstrap a hybrid AngularJS / Angular application. |
||||||
Parameters
Returns |
||||||
This |
||||||
Example
|
Allows AngularJS service to be accessible from Angular. |
||||
Parameters
|
||||
Example
|
Allows Angular service to be accessible from AngularJS. |
||
Parameters
Returns
|
||
Example
|
Usage notes
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.
- AngularJS directives always execute inside AngularJS framework codebase regardless of where they are instantiated.
- Angular components always execute inside Angular framework codebase regardless of where they are instantiated.
- An AngularJS component can be upgraded to an Angular component. This creates an Angular directive, which bootstraps the AngularJS component directive in that location.
- An Angular component can be downgraded to an AngularJS component directive. This creates an AngularJS directive, which bootstraps the Angular 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 syntax.
- AngularJS is always bootstrapped first and owns the bottom most view.
- The new application is running in Angular zone, and therefore it no longer needs calls to
$apply()
.
Example
- const adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module), myCompilerOptions);
- const module = angular.module('myExample', []);
- module.directive('ng2Comp', adapter.downgradeNg2Component(Ng2Component));
-
- module.directive('ng1Hello', function() {
- return {
- scope: { title: '=' },
- template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'
- };
- });
-
-
- @Component({
- selector: 'ng2-comp',
- inputs: ['name'],
- template: 'ng2[<ng1-hello [title]="name">transclude</ng1-hello>](<ng-content></ng-content>)',
- directives:
- })
- class Ng2Component {
- }
-
- @NgModule({
- declarations: [Ng2Component, adapter.upgradeNg1Component('ng1Hello')],
- imports: [BrowserModule]
- })
- class MyNg2Module {}
-
-
- document.body.innerHTML = '<ng2-comp name="World">project</ng2-comp>';
-
- adapter.bootstrap(document.body, ['myExample']).ready(function() {
- expect(document.body.textContent).toEqual(
- "ng2[ng1[Hello World!](transclude)](project)");
- });