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

downgradeModulelink

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

A helper function for creating an AngularJS module that can bootstrap an Angular module\n\"on-demand\" (possibly lazily) when a downgraded component needs to be\ninstantiated.

\n\n

See more...

\n
\n \n \n \n\n
\n \n\n downgradeModule<T>(moduleFactoryOrBootstrapFn: NgModuleFactory<T> | ((extraProviders: StaticProvider[]) => Promise<NgModuleRef<T>>)): string\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n moduleFactoryOrBootstrapFn\n NgModuleFactory | ((extraProviders: StaticProvider[]) => Promise>)\n \n \n
\n\n \n
Returns
\n

string

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

Descriptionlink

\n

Part of the upgrade/static library for hybrid upgrade apps that\nsupport AOT compilation.

\n

It allows loading/bootstrapping the Angular part of a hybrid application lazily and not having to\npay the cost up-front. For example, you can have an AngularJS application that uses Angular for\nspecific routes and only instantiate the Angular modules if/when the user visits one of these\nroutes.

\n

The Angular module will be bootstrapped once (when requested for the first time) and the same\nreference will be used from that point onwards.

\n

downgradeModule() requires either an NgModuleFactory or a function:

\n\n

downgradeModule() returns the name of the created AngularJS wrapper module. You can use it to\ndeclare a dependency in your main AngularJS module.

\n\n// Alternatively, we could import and use an `NgModuleFactory` instead:\n// import {MyLazyAngularModuleNgFactory} from './my-lazy-angular-module.ngfactory';\nimport {platformBrowserDynamic} from '@angular/platform-browser-dynamic';\nimport {downgradeModule} from '@angular/upgrade/static';\n\n\n// The function that will bootstrap the Angular module (when/if necessary).\n// (This would be omitted if we provided an `NgModuleFactory` directly.)\nconst ng2BootstrapFn = (extraProviders: StaticProvider[]) =>\n platformBrowserDynamic(extraProviders).bootstrapModule(MyLazyAngularModule);\n\n\n// This AngularJS module represents the AngularJS pieces of the application.\nconst myMainAngularJsModule = angular.module('myMainAngularJsModule', [\n // We declare a dependency on the \"downgraded\" Angular module.\n downgradeModule(ng2BootstrapFn)\n // or\n // downgradeModule(MyLazyAngularModuleFactory)\n]);\n\n\n

For more details on how to use downgradeModule() see\nUpgrading for Performance.

\n\n

Further information available in the Usage Notes...

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

Usage noteslink

\n

Apart from UpgradeModule, you can use the rest of the upgrade/static helpers as usual to\nbuild a hybrid application. Note that the Angular pieces (e.g. downgraded services) will not be\navailable until the downgraded module has been bootstrapped, i.e. by instantiating a downgraded\ncomponent.

\n
\n

You cannot use downgradeModule() and UpgradeModule in the same hybrid application.
\nUse one or the other.

\n
\n

Differences with UpgradeModulelink

\n

Besides their different API, there are two important internal differences between\ndowngradeModule() and UpgradeModule that affect the behavior of hybrid applications:

\n
    \n
  1. Unlike UpgradeModule, downgradeModule() does not bootstrap the main AngularJS module\ninside the Angular zone.
  2. \n
  3. Unlike UpgradeModule, downgradeModule() does not automatically run a\n$digest() when changes are\ndetected in the Angular part of the application.
  4. \n
\n

What this means is that applications using UpgradeModule will run change detection more\nfrequently in order to ensure that both frameworks are properly notified about possible changes.\nThis will inevitably result in more change detection runs than necessary.

\n

downgradeModule(), on the other side, does not try to tie the two change detection systems as\ntightly, restricting the explicit change detection runs only to cases where it knows it is\nnecessary (e.g. when the inputs of a downgraded component change). This improves performance,\nespecially in change-detection-heavy applications, but leaves it up to the developer to manually\nnotify each framework as needed.

\n

For a more detailed discussion of the differences and their implications, see\nUpgrading for Performance.

\n
\n

You can manually trigger a change detection run in AngularJS using\nscope.$apply(...) or\n$rootScope.$digest().

\n

You can manually trigger a change detection run in Angular using ngZone.run(...).

\n
\n

Downgrading multiple moduleslink

\n

It is possible to downgrade multiple modules and include them in an AngularJS application. In\nthat case, each downgraded module will be bootstrapped when an associated downgraded component or\ninjectable needs to be instantiated.

\n

Things to keep in mind, when downgrading multiple modules:

\n
    \n
  • \n

    Each downgraded component/injectable needs to be explicitly associated with a downgraded\nmodule. See downgradeComponent() and downgradeInjectable() for more details.

    \n
  • \n
  • \n

    If you want some injectables to be shared among all downgraded modules, you can provide them as\nStaticProviders, when creating the PlatformRef (e.g. via platformBrowser or\nplatformBrowserDynamic).

    \n
  • \n
  • \n

    When using `bootstrapModule()` or\n`bootstrapModuleFactory()` to bootstrap the\ndowngraded modules, each one is considered a \"root\" module. As a consequence, a new instance\nwill be created for every injectable provided in \"root\" (via\n`providedIn`).\nIf this is not your intention, you can have a shared module (that will act as act as the \"root\"\nmodule) and create all downgraded modules using that module's injector:

    \n\nlet rootInjectorPromise: Promise<Injector>|null = null;\nconst getRootInjector = (extraProviders: StaticProvider[]) => {\n if (!rootInjectorPromise) {\n rootInjectorPromise = platformBrowserDynamic(extraProviders)\n .bootstrapModule(Ng2RootModule)\n .then(moduleRef => moduleRef.injector);\n }\n return rootInjectorPromise;\n};\n\nconst downgradedNg2AModule = downgradeModule(async (extraProviders: StaticProvider[]) => {\n const rootInjector = await getRootInjector(extraProviders);\n const moduleAFactory = await rootInjector.get(Compiler).compileModuleAsync(Ng2AModule);\n return moduleAFactory.create(rootInjector);\n});\nconst downgradedNg2BModule = downgradeModule(async (extraProviders: StaticProvider[]) => {\n const rootInjector = await getRootInjector(extraProviders);\n const moduleBFactory = await rootInjector.get(Compiler).compileModuleAsync(Ng2BModule);\n return moduleBFactory.create(rootInjector);\n});\n/* . . . */\nconst appModule =\n angular\n .module(\n 'exampleAppModule', [downgradedNg2AModule, downgradedNg2BModule, downgradedNg2CModule])\n\n\n
  • \n
\n\n
\n\n\n\n
\n
\n\n\n" }