This is the archived documentation for Angular v6. Please visit angular.io to see documentation for the current version of Angular.

downgradeInjectable

A helper function to allow an Angular service to be accessible from AngularJS.

See more...

downgradeInjectable(token: any): Function
      
      downgradeInjectable(token: any): Function
    

Parameters

token

an InjectionToken that identifies a service provided from Angular.

Returns

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

Description

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

This helper function returns a factory function that provides access to the Angular service identified by the token parameter.

Usage notes

Examples

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(textFormatter: TextFormatter) { // Change all the hero names to title case, using the "upgraded" AngularJS service this.heroes.forEach((hero: Hero) => hero.name = textFormatter.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); } }
      
      
  1. // This Angular service will be "downgraded" to be used in AngularJS
  2. @Injectable()
  3. class HeroesService {
  4. heroes: Hero[] = [
  5. {name: 'superman', description: 'The man of steel'},
  6. {name: 'wonder woman', description: 'Princess of the Amazons'},
  7. {name: 'thor', description: 'The hammer-wielding god'}
  8. ];
  9.  
  10. constructor(textFormatter: TextFormatter) {
  11. // Change all the hero names to title case, using the "upgraded" AngularJS service
  12. this.heroes.forEach((hero: Hero) => hero.name = textFormatter.titleCase(hero.name));
  13. }
  14.  
  15. addHero() {
  16. this.heroes =
  17. this.heroes.concat([{name: 'Kamala Khan', description: 'Epic shape-shifting healer'}]);
  18. }
  19.  
  20. removeHero(hero: Hero) { this.heroes = this.heroes.filter((item: Hero) => item !== hero); }
  21. }

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: TextFormatter, useFactory: (i: any) => i.get('textFormatter'), 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 { }
      
      
  1. // This NgModule represents the Angular pieces of the application
  2. @NgModule({
  3. declarations: [Ng2HeroesComponent, Ng1HeroComponentWrapper],
  4. providers: [
  5. HeroesService,
  6. // Register an Angular provider whose value is the "upgraded" AngularJS service
  7. {provide: TextFormatter, useFactory: (i: any) => i.get('textFormatter'), deps: ['$injector']}
  8. ],
  9. // All components that are to be "downgraded" must be declared as `entryComponents`
  10. entryComponents: [Ng2HeroesComponent],
  11. // We must import `UpgradeModule` to get access to the AngularJS core services
  12. imports: [BrowserModule, UpgradeModule]
  13. })
  14. class Ng2AppModule {
  15. }

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) as any);
      
      // Register an AngularJS service, whose value is the "downgraded" Angular injectable.
ng1AppModule.factory('heroesService', downgradeInjectable(HeroesService) as any);
    

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