downgradeInjectable

function

npm Package @angular/upgrade
Module import { downgradeInjectable } from '@angular/upgrade/static';
Source upgrade/src/common/downgrade_injectable.ts

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

Allow an Angular service to be accessible from AngularJS.

function downgradeInjectable(token: any): Function;

How To Use

First ensure that the service to be downgraded is provided in an NgModule that will be part of the upgrade application. For example, let's assume we have defined HeroesService

// This Angular service will be "downgraded" to be used in AngularJS
@Injectable()
class HeroesService {
  heroes: Hero[] = [
    {name: 'superman', description: 'The man of steel'},
    {name: 'wonder woman', description: 'Princess of the Amazons'},
    {name: 'thor', description: 'The hammer-wielding god'}
  ];

  constructor(@Inject('titleCase') titleCase: (v: string) => string) {
    // Change all the hero names to title case, using the "upgraded" AngularJS service
    this.heroes.forEach((hero: Hero) => hero.name = titleCase(hero.name));
  }

  addHero() {
    this.heroes =
        this.heroes.concat([{name: 'Kamala Khan', description: 'Epic shape-shifting healer'}]);
  }

  removeHero(hero: Hero) { this.heroes = this.heroes.filter((item: Hero) => item !== hero); }
}

and that we have included this in our upgrade app NgModule

// This NgModule represents the Angular pieces of the application
@NgModule({
  declarations: [Ng2HeroesComponent, Ng1HeroComponentWrapper],
  providers: [
    HeroesService,
    // Register an Angular provider whose value is the "upgraded" AngularJS service
    {provide: 'titleCase', useFactory: (i: any) => i.get('titleCase'), deps: ['$injector']}
  ],
  // All components that are to be "downgraded" must be declared as `entryComponents`
  entryComponents: [Ng2HeroesComponent],
  // We must import `UpgradeModule` to get access to the AngularJS core services
  imports: [BrowserModule, UpgradeModule]
})
class Ng2AppModule {
  ngDoBootstrap() { /* this is a placeholder to stop the boostrapper from complaining */
  }
}

Now we can register the downgradeInjectable factory function for the service on an AngularJS module.

// Register an AngularJS service, whose value is the "downgraded" Angular injectable.
ng1AppModule.factory('heroesService', downgradeInjectable(HeroesService));

Inside an AngularJS component's controller we can get hold of the downgraded service via the name we gave when downgrading.

// This is our top level application component
ng1AppModule.component('exampleApp', {
  // We inject the "downgraded" HeroesService into this AngularJS component
  // (We don't need the `HeroesService` type for AngularJS DI - it just helps with TypeScript
  // compilation)
  controller: [
    'heroesService', function(heroesService: HeroesService) { this.heroesService = heroesService; }
  ],
  // This template make use of the downgraded `ng2-heroes` component
  // Note that because its element is compiled by AngularJS we must use kebab-case attributes for
  // inputs and outputs
  template: `<link rel="stylesheet" href="./styles.css">
             <ng2-heroes [heroes]="$ctrl.heroesService.heroes" (add-hero)="$ctrl.heroesService.addHero()" (remove-hero)="$ctrl.heroesService.removeHero($event)">
               <h1>Heroes</h1>
               <p class="extra">There are {{ $ctrl.heroesService.heroes.length }} heroes.</p>
             </ng2-heroes>`
});

Description

Takes a token that identifies a service provided from Angular.

Returns a factory function that can be used to register the service on an AngularJS module.

The factory function provides access to the Angular service that is identified by the token parameter.

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v4.angular.io/api/upgrade/static/downgradeInjectable