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

downgradeInjectablelink

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

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

\n\n

See more...

\n
\n \n \n \n\n
\n \n\n downgradeInjectable(token: any, downgradedModule: string = ''): Function\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n token\n any\n

an InjectionToken that identifies a service provided from Angular.

\n\n
\n \n downgradedModule\n string\n

the name of the downgraded module (if any) that the injectable\n\"belongs to\", as returned by a call to downgradeModule(). It is the module, whose injector will\nbe used for instantiating the injectable.
\n(This option is only necessary when using downgradeModule() to downgrade more than one Angular\nmodule.)

\n

Optional. Default is ''.

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

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

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

Descriptionlink

\n

Part of the upgrade/static\nlibrary for hybrid upgrade apps that support AOT compilation

\n

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

\n\n

Further information available in the Usage Notes...

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

Usage noteslink

\n

Exampleslink

\n

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

\n\n// This Angular service will be \"downgraded\" to be used in AngularJS\n@Injectable()\nexport class HeroesService {\n heroes: Hero[] = [\n {name: 'superman', description: 'The man of steel'},\n {name: 'wonder woman', description: 'Princess of the Amazons'},\n {name: 'thor', description: 'The hammer-wielding god'}\n ];\n\n constructor(textFormatter: TextFormatter) {\n // Change all the hero names to title case, using the \"upgraded\" AngularJS service\n this.heroes.forEach((hero: Hero) => hero.name = textFormatter.titleCase(hero.name));\n }\n\n addHero() {\n this.heroes =\n this.heroes.concat([{name: 'Kamala Khan', description: 'Epic shape-shifting healer'}]);\n }\n\n removeHero(hero: Hero) {\n this.heroes = this.heroes.filter((item: Hero) => item !== hero);\n }\n}\n\n\n

and that we have included this in our upgrade app NgModule

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

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

\n\n// Register an AngularJS service, whose value is the \"downgraded\" Angular injectable.\nng1AppModule.factory('heroesService', downgradeInjectable(HeroesService) as any);\n\n\n

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

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

When using downgradeModule(), downgraded injectables will not be available until the Angular\nmodule that provides them is instantiated. In order to be safe, you need to ensure that the\ndowngraded injectables are not used anywhere outside the part of the app where it is\nguaranteed that their module has been instantiated.

\n

For example, it is OK to use a downgraded service in an upgraded component that is only used\nfrom a downgraded Angular component provided by the same Angular module as the injectable, but\nit is not OK to use it in an AngularJS component that may be used independently of Angular or\nuse it in a downgraded Angular component from a different module.

\n
\n\n
\n\n\n\n
\n
\n\n\n" }