Skip to content

Commit e9cb045

Browse files
gkalpakalxhub
authored andcommitted
feat(upgrade): more closely align UpgradeModule#bootstrap() with angular.bootstrap() (angular#46214)
Previously, [UpgradeModule#bootstrap()][1], while being a replacement for and accepting the same arguments as [angular.bootstrap()][2], did not return the same value as `angular.bootstrap()` (i.e. the AngularJS injector in most cases). This made it less straight forward to migrate some usecases that relied on the return value of `.bootstrap()`. The work-around was to access the injector via [UpgradeModule#$injector][3] (after the app had been bootstrapped with `UpgradeModule#bootstrap()`). This commit addresses this by ensuring `UpgradeModule#bootstrap()` returns the same value as `angular.bootstrap()`, making it easier to replace the latter with the former. [1]: https://angular.io/api/upgrade/static/UpgradeModule#bootstrap [2]: https://docs.angularjs.org/api/ng/function/angular.bootstrap [3]: https://angular.io/api/upgrade/static/UpgradeModule#%24injector Fixes angular#46211 PR Close angular#46214
1 parent 4e5e312 commit e9cb045

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

goldens/public-api/upgrade/static/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class UpgradeModule {
7575
injector: Injector,
7676
ngZone: NgZone,
7777
platformRef: PlatformRef);
78-
bootstrap(element: Element, modules?: string[], config?: any): void;
78+
bootstrap(element: Element, modules?: string[], config?: any): any;
7979
injector: Injector;
8080
ngZone: NgZone;
8181
// (undocumented)

packages/upgrade/static/src/upgrade_module.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ import {NgAdapterInjector} from './util';
9191
* This class is an `NgModule`, which you import to provide AngularJS core services,
9292
* and has an instance method used to bootstrap the hybrid upgrade application.
9393
*
94-
* * Core AngularJS services
94+
* * Core AngularJS services<br />
9595
* Importing this `NgModule` will add providers for the core
9696
* [AngularJS services](https://docs.angularjs.org/api/ng/service) to the root injector.
9797
*
98-
* * Bootstrap
98+
* * Bootstrap<br />
9999
* The runtime instance of this class contains a {@link UpgradeModule#bootstrap `bootstrap()`}
100100
* method, which you use to bootstrap the top level AngularJS module onto an element in the
101101
* DOM for the hybrid upgrade app.
@@ -170,9 +170,12 @@ export class UpgradeModule {
170170
* @param element the element on which to bootstrap the AngularJS application
171171
* @param [modules] the AngularJS modules to bootstrap for this application
172172
* @param [config] optional extra AngularJS bootstrap configuration
173+
* @return The value returned by
174+
* [angular.bootstrap()](https://docs.angularjs.org/api/ng/function/angular.bootstrap).
173175
*/
174176
bootstrap(
175-
element: Element, modules: string[] = [], config?: any /*angular.IAngularBootstrapConfig*/) {
177+
element: Element, modules: string[] = [], config?: any /*angular.IAngularBootstrapConfig*/):
178+
any /*ReturnType<typeof angular.bootstrap>*/ {
176179
const INIT_MODULE_NAME = UPGRADE_MODULE_NAME + '.init';
177180

178181
// Create an ng1 module to bootstrap
@@ -303,9 +306,7 @@ export class UpgradeModule {
303306
windowAngular.resumeBootstrap = undefined;
304307

305308
// Bootstrap the AngularJS application inside our zone
306-
this.ngZone.run(() => {
307-
bootstrap(element, [upgradeModule.name], config);
308-
});
309+
const returnValue = this.ngZone.run(() => bootstrap(element, [upgradeModule.name], config));
309310

310311
// Patch resumeBootstrap() to run inside the ngZone
311312
if (windowAngular.resumeBootstrap) {
@@ -317,5 +318,7 @@ export class UpgradeModule {
317318
return ngZone.run(() => windowAngular.resumeBootstrap.apply(this, args));
318319
};
319320
}
321+
322+
return returnValue;
320323
}
321324
}

packages/upgrade/static/test/integration/upgrade_module_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,29 @@ withEachNg1Version(() => {
125125

126126
expect(bootstrapSpy).toHaveBeenCalledOnceWith(element, jasmine.any(Array), config);
127127
});
128+
129+
it('should forward the return value of `angular.bootstrap()`', async () => {
130+
// Set up spies.
131+
const bootstrapSpy = spyOn(getAngularJSGlobal(), 'bootstrap').and.callThrough();
132+
133+
// Define `Ng2Module`.
134+
@NgModule({
135+
imports: [BrowserModule, UpgradeModule],
136+
})
137+
class Ng2Module {
138+
ngDoBootstrap() {}
139+
}
140+
141+
// Bootstrap the ng2 app.
142+
const appRef = await platformBrowserDynamic().bootstrapModule(Ng2Module);
143+
const upgrade = appRef.injector.get(UpgradeModule);
144+
145+
// Bootstrap the hybrid app.
146+
const retValue = upgrade.bootstrap(html(`<ng2></ng2>`), []);
147+
148+
expect(retValue).toBe(bootstrapSpy.calls.mostRecent().returnValue);
149+
expect(retValue).toBe(upgrade.$injector); // In most cases, it will be the ng1 injector.
150+
});
128151
});
129152
});
130153
});

0 commit comments

Comments
 (0)