downgradeComponent

Experimental

Function

What it does

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

Allows an Angular component to be used from AngularJS.

How to use

Let's assume that you have an Angular component called ng2Heroes that needs to be made available in AngularJS templates.

  1. // This Angular component will be "downgraded" to be used in AngularJS
  2. @Component({
  3. selector: 'ng2-heroes',
  4. // This template uses the upgraded `ng1-hero` component
  5. // Note that because its element is compiled by Angular we must use camelCased attribute names
  6. template: `<header><ng-content selector="h1"></ng-content></header>
  7. <ng-content selector=".extra"></ng-content>
  8. <div *ngFor="let hero of heroes">
  9. <ng1-hero [hero]="hero" (onRemove)="removeHero.emit(hero)"><strong>Super Hero</strong></ng1-hero>
  10. </div>
  11. <button (click)="addHero.emit()">Add Hero</button>`,
  12. })
  13. class Ng2HeroesComponent {
  14. @Input() heroes: Hero[];
  15. @Output() addHero = new EventEmitter();
  16. @Output() removeHero = new EventEmitter();
  17. }

We must create an AngularJS directive that will make this Angular component available inside AngularJS templates. The downgradeComponent() function returns a factory function that we can use to define the AngularJS directive that wraps the "downgraded" component.

  1. // This is directive will act as the interface to the "downgraded" Angular component
  2. ng1AppModule.directive('ng2Heroes', downgradeComponent({component: Ng2HeroesComponent}));

Class Export

exported from upgrade/static/index defined in upgrade/static/src/common/downgrade_component.ts