UpgradeAdapter
Use UpgradeAdapter
to allow AngularJS and Angular to coexist in a single application.
class UpgradeAdapter {}
downgradeNg2Component
Function
Allows Angular Component to be used from AngularJS.
Use downgradeNg2Component
to create an AngularJS Directive Definition Factory from
Angular Component. The adapter will bootstrap Angular component from within the
AngularJS template.
Function
Mental Model
- The component is instantiated by being listed in AngularJS template. This means that the host element is controlled by AngularJS, but the component's view will be controlled by Angular.
- Even thought the component is instantiated in AngularJS, it will be using Angular syntax. This has to be done, this way because we must follow Angular components do not declare how the attributes should be interpreted.
ng-model
is controlled by AngularJS and communicates with the downgraded Angular component by way of theControlValueAccessor
interface from
upgradeNg1Component
Type<any>
Allows AngularJS Component to be used from Angular.
Use upgradeNg1Component
to create an Angular component from AngularJS Component
directive. The adapter will bootstrap AngularJS component from within the Angular
template.
string
Type<any>
Mental Model
- The component is instantiated by being listed in Angular template. This means that the host element is controlled by Angular, but the component's view will be controlled by AngularJS.
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, 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.
Example
const adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module));const module = angular.module('myExample', []);module.directive('greet', function() { return { scope: {salutation: '=', name: '=' }, template: '{{salutation}} {{name}}! - <span ng-transclude></span>' };});module.directive('ng2', adapter.downgradeNg2Component(Ng2Component));@Component({ selector: 'ng2', template: 'ng2 template: <greet salutation="Hello" [name]="world">text</greet>'})class Ng2Component {}@NgModule({ declarations: [Ng2Component, adapter.upgradeNg1Component('greet')], imports: [BrowserModule]})class MyNg2Module {}document.body.innerHTML = '<ng2></ng2>';adapter.bootstrap(document.body, ['myExample']).ready(function() { expect(document.body.textContent).toEqual("ng2 template: Hello world! - text");});
registerForNg1Tests
Registers the adapter's AngularJS upgrade module for unit testing in AngularJS.
Use this instead of angular.mock.module()
to load the upgrade module into
the AngularJS testing injector.
string[] | undefined
any AngularJS modules that the upgrade module should depend upon
UpgradeAdapterRef
Example
const upgradeAdapter = new UpgradeAdapter(MyNg2Module);// configure the adapter with upgrade/downgrade components and servicesupgradeAdapter.downgradeNg2Component(MyComponent);let upgradeAdapterRef: UpgradeAdapterRef;let $compile, $rootScope;// We must register the adapter before any calls to `inject()`beforeEach(() => { upgradeAdapterRef = upgradeAdapter.registerForNg1Tests(['heroApp']);});beforeEach(inject((_$compile_, _$rootScope_) => { $compile = _$compile_; $rootScope = _$rootScope_;}));it("says hello", (done) => { upgradeAdapterRef.ready(() => { const element = $compile("<my-component></my-component>")($rootScope); $rootScope.$apply(); expect(element.html()).toContain("Hello World"); done(); })});
bootstrap
Bootstrap a hybrid AngularJS / Angular application.
This bootstrap
method is a direct replacement (takes same arguments) for AngularJS
bootstrap
method. Unlike
AngularJS, this bootstrap is asynchronous.
Element
any[] | undefined
any
UpgradeAdapterRef
Example
const adapter = new UpgradeAdapter(MyNg2Module);const module = angular.module('myExample', []);module.directive('ng2', adapter.downgradeNg2Component(Ng2));module.directive('ng1', function() { return { scope: { title: '=' }, template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)' };});@Component({ selector: 'ng2', inputs: ['name'], template: 'ng2[<ng1 [title]="name">transclude</ng1>](<ng-content></ng-content>)'})class Ng2 {}@NgModule({ declarations: [Ng2, adapter.upgradeNg1Component('ng1')], imports: [BrowserModule]})class MyNg2Module {}document.body.innerHTML = '<ng2 name="World">project</ng2>';adapter.bootstrap(document.body, ['myExample']).ready(function() { expect(document.body.textContent).toEqual( "ng2[ng1[Hello World!](transclude)](project)");});
upgradeNg1Provider
void
Allows AngularJS service to be accessible from Angular.
string
{ asToken: any; } | undefined
void
Example
class Login { ... }class Server { ... }@Injectable()class Example { constructor(@Inject('server') server, login: Login) { ... }}const module = angular.module('myExample', []);module.service('server', Server);module.service('login', Login);const adapter = new UpgradeAdapter(MyNg2Module);adapter.upgradeNg1Provider('server');adapter.upgradeNg1Provider('login', {asToken: Login});adapter.bootstrap(document.body, ['myExample']).ready((ref) => { const example: Example = ref.ng2Injector.get(Example);});
downgradeNg2Provider
Function
Allows Angular service to be accessible from AngularJS.
any
Function
Example
class Example {}const adapter = new UpgradeAdapter(MyNg2Module);const module = angular.module('myExample', []);module.factory('example', adapter.downgradeNg2Provider(Example));adapter.bootstrap(document.body, ['myExample']).ready((ref) => { const example: Example = ref.ng1Injector.get('example');});