{ "id": "api/upgrade/UpgradeAdapter", "title": "UpgradeAdapter", "contents": "\n\n
\n
\n
\n \n API > @angular/upgrade\n
\n \n
\n \n
\n

UpgradeAdapterlink

\n \n \n \n \n \n
\n \n \n\n
\n \n
\n

Use UpgradeAdapter to allow AngularJS and Angular to coexist in a single application.

\n\n

See more...

\n
\n \n \n
\n

Deprecated: Deprecated since v5. Use upgrade/static instead, which also supports\nAhead-of-Time compilation.

\n\n
\n\n \n
\n\nclass UpgradeAdapter {\n constructor(ng2AppModule: Type<any>, compilerOptions?: CompilerOptions)\n downgradeNg2Component(component: Type<any>): Function\n upgradeNg1Component(name: string): Type<any>\n registerForNg1Tests(modules?: string[]): UpgradeAdapterRef\n bootstrap(element: Element, modules?: any[], config?: IAngularBootstrapConfig): UpgradeAdapterRef\n upgradeNg1Provider(name: string, options?: { asToken: any; })\n downgradeNg2Provider(token: any): Function\n}\n\n\n \n \n\n
\n\n\n \n\n \n \n
\n

Descriptionlink

\n

The UpgradeAdapter allows:\n1. creation of Angular component from AngularJS component directive\n(See [UpgradeAdapter#upgradeNg1Component()])\n2. creation of AngularJS directive from Angular component.\n(See [UpgradeAdapter#downgradeNg2Component()])\n3. Bootstrapping of a hybrid Angular application which contains both of the frameworks\ncoexisting in a single application.

\n\n

Further information available in the Usage Notes...

\n
\n\n \n\n\n\n\n\n\n

Constructorlink

\n\n\n \n \n \n \n \n \n \n \n\n \n\n \n \n
\n
\n \n\n constructor(ng2AppModule: Type<any>, compilerOptions?: CompilerOptions)\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n ng2AppModule\n Type\n \n \n
\n \n compilerOptions\n CompilerOptions\n

Optional. Default is undefined.

\n \n
\n\n \n\n\n \n\n \n
\n
\n\n\n\n\n\n\n\n\n
\n

Methodslink

\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n \n \n
\n
\n

\n downgradeNg2Component()\n \n link

\n \n
\n
\n

Allows Angular Component to be used from AngularJS.

\n\n
\n
\n \n\n downgradeNg2Component(component: Type<any>): Function\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n component\n Type\n \n \n
\n\n \n
Returns
\n

Function

\n\n \n\n\n \n\n \n
\n
\n

Use downgradeNg2Component to create an AngularJS Directive Definition Factory from\nAngular Component. The adapter will bootstrap Angular component from within the\nAngularJS template.

\n\n
\n

Usage Noteslink

\n
Mental Modellink
\n
    \n
  1. The component is instantiated by being listed in AngularJS template. This means that the\nhost element is controlled by AngularJS, but the component's view will be controlled by\nAngular.
  2. \n
  3. Even thought the component is instantiated in AngularJS, it will be using Angular\nsyntax. This has to be done, this way because we must follow Angular components do not\ndeclare how the attributes should be interpreted.
  4. \n
  5. ng-model is controlled by AngularJS and communicates with the downgraded Angular component\nby way of the ControlValueAccessor interface from @angular/forms. Only components that\nimplement this interface are eligible.
  6. \n
\n
Supported Featureslink
\n
    \n
  • \n

    Bindings:

    \n
      \n
    • Attribute: <comp name=\"World\">
    • \n
    • Interpolation: <comp greeting=\"Hello {{name}}!\">
    • \n
    • Expression: <comp [name]=\"username\">
    • \n
    • Event: <comp (close)=\"doSomething()\">
    • \n
    • ng-model: <comp ng-model=\"name\">
    • \n
    \n
  • \n
  • Content projection: yes
  • \n
\n
Examplelink
\n\nconst adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module));\nconst module = angular.module('myExample', []);\nmodule.directive('greet', adapter.downgradeNg2Component(Greeter));\n\n@Component({\n selector: 'greet',\n template: '{{salutation}} {{name}}! - <ng-content></ng-content>'\n})\nclass Greeter {\n @Input() salutation: string;\n @Input() name: string;\n}\n\n@NgModule({\n declarations: [Greeter],\n imports: [BrowserModule]\n})\nclass MyNg2Module {}\n\ndocument.body.innerHTML =\n 'ng1 template: <greet salutation=\"Hello\" [name]=\"world\">text</greet>';\n\nadapter.bootstrap(document.body, ['myExample']).ready(function() {\n expect(document.body.textContent).toEqual(\"ng1 template: Hello world! - text\");\n});\n\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n \n \n
\n
\n

\n upgradeNg1Component()\n \n link

\n \n
\n
\n

Allows AngularJS Component to be used from Angular.

\n\n
\n
\n \n\n upgradeNg1Component(name: string): Type<any>\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n name\n string\n \n \n
\n\n \n
Returns
\n

Type<any>

\n\n \n\n\n \n\n \n
\n
\n

Use upgradeNg1Component to create an Angular component from AngularJS Component\ndirective. The adapter will bootstrap AngularJS component from within the Angular\ntemplate.

\n\n
\n

Usage Noteslink

\n
Mental Modellink
\n
    \n
  1. The component is instantiated by being listed in Angular template. This means that the\nhost element is controlled by Angular, but the component's view will be controlled by\nAngularJS.
  2. \n
\n
Supported Featureslink
\n
    \n
  • \n

    Bindings:

    \n
      \n
    • Attribute: <comp name=\"World\">
    • \n
    • Interpolation: <comp greeting=\"Hello {{name}}!\">
    • \n
    • Expression: <comp [name]=\"username\">
    • \n
    • Event: <comp (close)=\"doSomething()\">
    • \n
    \n
  • \n
  • Transclusion: yes
  • \n
  • \n

    Only some of the features of\nDirective Definition Object are\nsupported:

    \n
      \n
    • compile: not supported because the host element is owned by Angular, which does\nnot allow modifying DOM structure during compilation.
    • \n
    • controller: supported. (NOTE: injection of $attrs and $transclude is not supported.)
    • \n
    • controllerAs: supported.
    • \n
    • bindToController: supported.
    • \n
    • link: supported. (NOTE: only pre-link function is supported.)
    • \n
    • name: supported.
    • \n
    • priority: ignored.
    • \n
    • replace: not supported.
    • \n
    • require: supported.
    • \n
    • restrict: must be set to 'E'.
    • \n
    • scope: supported.
    • \n
    • template: supported.
    • \n
    • templateUrl: supported.
    • \n
    • terminal: ignored.
    • \n
    • transclude: supported.
    • \n
    \n
  • \n
\n
Examplelink
\n\nconst adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module));\nconst module = angular.module('myExample', []);\n\nmodule.directive('greet', function() {\n return {\n scope: {salutation: '=', name: '=' },\n template: '{{salutation}} {{name}}! - <span ng-transclude></span>'\n };\n});\n\nmodule.directive('ng2', adapter.downgradeNg2Component(Ng2Component));\n\n@Component({\n selector: 'ng2',\n template: 'ng2 template: <greet salutation=\"Hello\" [name]=\"world\">text</greet>'\n})\nclass Ng2Component {\n}\n\n@NgModule({\n declarations: [Ng2Component, adapter.upgradeNg1Component('greet')],\n imports: [BrowserModule]\n})\nclass MyNg2Module {}\n\ndocument.body.innerHTML = '<ng2></ng2>';\n\nadapter.bootstrap(document.body, ['myExample']).ready(function() {\n expect(document.body.textContent).toEqual(\"ng2 template: Hello world! - text\");\n});\n\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n\n \n \n \n \n
\n
\n

\n registerForNg1Tests()\n \n link

\n \n
\n
\n

Registers the adapter's AngularJS upgrade module for unit testing in AngularJS.\nUse this instead of angular.mock.module() to load the upgrade module into\nthe AngularJS testing injector.

\n\n
\n
\n \n\n registerForNg1Tests(modules?: string[]): UpgradeAdapterRef\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n modules\n string[]\n

any AngularJS modules that the upgrade module should depend upon

\n

Optional. Default is undefined.

\n\n
\n\n \n
Returns
\n

UpgradeAdapterRef: an UpgradeAdapterRef, which lets you register a ready() callback to\nrun assertions once the Angular components are ready to test through AngularJS.

\n\n \n\n\n \n\n \n
\n
\n

Usage Noteslink

\n
Examplelink
\n\nconst upgradeAdapter = new UpgradeAdapter(MyNg2Module);\n\n// configure the adapter with upgrade/downgrade components and services\nupgradeAdapter.downgradeNg2Component(MyComponent);\n\nlet upgradeAdapterRef: UpgradeAdapterRef;\nlet $compile, $rootScope;\n\n// We must register the adapter before any calls to `inject()`\nbeforeEach(() => {\n upgradeAdapterRef = upgradeAdapter.registerForNg1Tests(['heroApp']);\n});\n\nbeforeEach(inject((_$compile_, _$rootScope_) => {\n $compile = _$compile_;\n $rootScope = _$rootScope_;\n}));\n\nit(\"says hello\", (done) => {\n upgradeAdapterRef.ready(() => {\n const element = $compile(\"<my-component></my-component>\")($rootScope);\n $rootScope.$apply();\n expect(element.html()).toContain(\"Hello World\");\n done();\n })\n});\n\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n \n \n
\n
\n

\n bootstrap()\n \n link

\n \n
\n
\n

Bootstrap a hybrid AngularJS / Angular application.

\n\n
\n
\n \n\n bootstrap(element: Element, modules?: any[], config?: IAngularBootstrapConfig): UpgradeAdapterRef\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n element\n Element\n \n \n
\n \n modules\n any[]\n

Optional. Default is undefined.

\n \n
\n \n config\n IAngularBootstrapConfig\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

UpgradeAdapterRef

\n\n \n\n\n \n\n \n
\n
\n

This bootstrap method is a direct replacement (takes same arguments) for AngularJS\nbootstrap method. Unlike\nAngularJS, this bootstrap is asynchronous.

\n\n
\n

Usage Noteslink

\n
Examplelink
\n\nconst adapter = new UpgradeAdapter(MyNg2Module);\nconst module = angular.module('myExample', []);\nmodule.directive('ng2', adapter.downgradeNg2Component(Ng2));\n\nmodule.directive('ng1', function() {\n return {\n scope: { title: '=' },\n template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'\n };\n});\n\n\n@Component({\n selector: 'ng2',\n inputs: ['name'],\n template: 'ng2[<ng1 [title]=\"name\">transclude</ng1>](<ng-content></ng-content>)'\n})\nclass Ng2 {\n}\n\n@NgModule({\n declarations: [Ng2, adapter.upgradeNg1Component('ng1')],\n imports: [BrowserModule]\n})\nclass MyNg2Module {}\n\ndocument.body.innerHTML = '<ng2 name=\"World\">project</ng2>';\n\nadapter.bootstrap(document.body, ['myExample']).ready(function() {\n expect(document.body.textContent).toEqual(\n \"ng2[ng1[Hello World!](transclude)](project)\");\n});\n\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n\n \n \n \n \n
\n
\n

\n upgradeNg1Provider()\n \n link

\n \n
\n
\n

Allows AngularJS service to be accessible from Angular.

\n\n
\n
\n \n\n upgradeNg1Provider(name: string, options?: { asToken: any; })\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n name\n string\n \n \n
\n \n options\n { asToken: any; }\n

Optional. Default is undefined.

\n \n
\n\n \n\n\n \n\n \n
\n
\n

Usage Noteslink

\n
Examplelink
\n\nclass Login { ... }\nclass Server { ... }\n\n@Injectable()\nclass Example {\n constructor(@Inject('server') server, login: Login) {\n ...\n }\n}\n\nconst module = angular.module('myExample', []);\nmodule.service('server', Server);\nmodule.service('login', Login);\n\nconst adapter = new UpgradeAdapter(MyNg2Module);\nadapter.upgradeNg1Provider('server');\nadapter.upgradeNg1Provider('login', {asToken: Login});\n\nadapter.bootstrap(document.body, ['myExample']).ready((ref) => {\n const example: Example = ref.ng2Injector.get(Example);\n});\n\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n\n \n \n \n \n
\n
\n

\n downgradeNg2Provider()\n \n link

\n \n
\n
\n

Allows Angular service to be accessible from AngularJS.

\n\n
\n
\n \n\n downgradeNg2Provider(token: any): Function\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n token\n any\n \n \n
\n\n \n
Returns
\n

Function

\n\n \n\n\n \n\n \n
\n
\n

Usage Noteslink

\n
Examplelink
\n\nclass Example {\n}\n\nconst adapter = new UpgradeAdapter(MyNg2Module);\n\nconst module = angular.module('myExample', []);\nmodule.factory('example', adapter.downgradeNg2Provider(Example));\n\nadapter.bootstrap(document.body, ['myExample']).ready((ref) => {\n const example: Example = ref.ng1Injector.get('example');\n});\n\n\n
\n\n \n
\n\n\n\n \n
\n

Usage noteslink

\n

Mental Modellink

\n

When reasoning about how a hybrid application works it is useful to have a mental model which\ndescribes what is happening and explains what is happening at the lowest level.

\n
    \n
  1. There are two independent frameworks running in a single application, each framework treats\nthe other as a black box.
  2. \n
  3. Each DOM element on the page is owned exactly by one framework. Whichever framework\ninstantiated the element is the owner. Each framework only updates/interacts with its own\nDOM elements and ignores others.
  4. \n
  5. AngularJS directives always execute inside AngularJS framework codebase regardless of\nwhere they are instantiated.
  6. \n
  7. Angular components always execute inside Angular framework codebase regardless of\nwhere they are instantiated.
  8. \n
  9. An AngularJS component can be upgraded to an Angular component. This creates an\nAngular directive, which bootstraps the AngularJS component directive in that location.
  10. \n
  11. An Angular component can be downgraded to an AngularJS component directive. This creates\nan AngularJS directive, which bootstraps the Angular component in that location.
  12. \n
  13. Whenever an adapter component is instantiated the host element is owned by the framework\ndoing the instantiation. The other framework then instantiates and owns the view for that\ncomponent. This implies that component bindings will always follow the semantics of the\ninstantiation framework. The syntax is always that of Angular syntax.
  14. \n
  15. AngularJS is always bootstrapped first and owns the bottom most view.
  16. \n
  17. The new application is running in Angular zone, and therefore it no longer needs calls to\n$apply().
  18. \n
\n

Examplelink

\n\nconst adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module), myCompilerOptions);\nconst module = angular.module('myExample', []);\nmodule.directive('ng2Comp', adapter.downgradeNg2Component(Ng2Component));\n\nmodule.directive('ng1Hello', function() {\n return {\n scope: { title: '=' },\n template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'\n };\n});\n\n\n@Component({\n selector: 'ng2-comp',\n inputs: ['name'],\n template: 'ng2[<ng1-hello [title]=\"name\">transclude</ng1-hello>](<ng-content></ng-content>)',\n directives:\n})\nclass Ng2Component {\n}\n\n@NgModule({\n declarations: [Ng2Component, adapter.upgradeNg1Component('ng1Hello')],\n imports: [BrowserModule]\n})\nclass MyNg2Module {}\n\n\ndocument.body.innerHTML = '<ng2-comp name=\"World\">project</ng2-comp>';\n\nadapter.bootstrap(document.body, ['myExample']).ready(function() {\n expect(document.body.textContent).toEqual(\n \"ng2[ng1[Hello World!](transclude)](project)\");\n});\n\n\n
\n\n\n\n
\n
\n\n\n" }