Skip to content

Commit 8a67770

Browse files
umairhmdylhunn
authored andcommitted
docs(core): improve applicationref.bootstrap docs (angular#42407)
add proper examples and update usage notes add references to the method at relevant places in the docs PR Close angular#42407
1 parent 9498da1 commit 8a67770

File tree

4 files changed

+104
-16
lines changed

4 files changed

+104
-16
lines changed

aio/content/guide/bootstrapping.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ most applications have only one component tree and bootstrap a single root compo
165165
This one root component is usually called `AppComponent` and is in the
166166
root module's `bootstrap` array.
167167

168-
168+
In a situation where you want to bootstrap a component based on an API response,
169+
or you want to mount the `AppComponent` in a different DOM node that doesn't match
170+
the component selector, please refer to `ApplicationRef.bootstrap()`
171+
documentation.
169172

170173
## More about Angular Modules
171174

packages/core/src/application_ref.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -682,20 +682,41 @@ export class ApplicationRef {
682682
}
683683

684684
/**
685-
* Bootstrap a new component at the root level of the application.
685+
* Bootstrap a component onto the element identified by its selector or, optionally, to a
686+
* specified element.
686687
*
687688
* @usageNotes
688689
* ### Bootstrap process
689690
*
690-
* When bootstrapping a new root component into an application, Angular mounts the
691-
* specified application component onto DOM elements identified by the componentType's
692-
* selector and kicks off automatic change detection to finish initializing the component.
691+
* When bootstrapping a component, Angular mounts it onto a target DOM element
692+
* and kicks off automatic change detection. The target DOM element can be
693+
* provided using the `rootSelectorOrNode` argument.
693694
*
694-
* Optionally, a component can be mounted onto a DOM element that does not match the
695-
* componentType's selector.
695+
* If the target DOM element is not provided, Angular tries to find one on a page
696+
* using the `selector` of the component that is being bootstrapped
697+
* (first matched element is used).
696698
*
697699
* ### Example
698-
* {@example core/ts/platform/platform.ts region='longform'}
700+
*
701+
* Generally, we define the component to bootstrap in the `bootstrap` array of `NgModule`,
702+
* but it requires us to know the component while writing the application code.
703+
*
704+
* Imagine a situation where we have to wait for an API call to decide about the component to
705+
* bootstrap. We can use the `ngDoBootstrap` hook of the `NgModule` and call this method to
706+
* dynamically bootstrap a component.
707+
*
708+
* {@example core/ts/platform/platform.ts region='componentSelector'}
709+
*
710+
* Optionally, a component can be mounted onto a DOM element that does not match the
711+
* selector of the bootstrapped component.
712+
*
713+
* In the following example, we are providing a CSS selector to match the target element.
714+
*
715+
* {@example core/ts/platform/platform.ts region='cssSelector'}
716+
*
717+
* While in this example, we are providing reference to a DOM node.
718+
*
719+
* {@example core/ts/platform/platform.ts region='domNode'}
699720
*/
700721
bootstrap<C>(componentOrFactory: ComponentFactory<C>|Type<C>, rootSelectorOrNode?: string|any):
701722
ComponentRef<C> {

packages/core/src/metadata/do_boostrap.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import {ApplicationRef} from '../application_ref';
1919
* See ["Bootstrapping"](guide/bootstrapping) and ["Entry components"](guide/entry-components).
2020
*
2121
* @usageNotes
22+
* The example below uses `ApplicationRef.bootstrap()` to render the
23+
* `AppComponent` on the page.
24+
*
2225
* ```typescript
2326
* class AppModule implements DoBootstrap {
2427
* ngDoBootstrap(appRef: ApplicationRef) {

packages/examples/core/ts/platform/platform.ts

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,75 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Component, createPlatformFactory} from '@angular/core';
10-
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
9+
import {ApplicationRef, Component, DoBootstrap, NgModule, Type} from '@angular/core';
10+
import {BrowserModule} from '@angular/platform-browser';
1111

12-
// #docregion longform
13-
@Component({selector: 'app-root', template: 'Hello World'})
14-
class MyApp {
12+
@Component({
13+
selector: 'app-root',
14+
template: ` <h1>Component One</h1> `,
15+
})
16+
export class ComponentOne {
1517
}
1618

17-
const myPlatformFactory = createPlatformFactory(platformBrowserDynamic, 'myPlatform');
18-
myPlatformFactory().bootstrapModule(MyApp);
19-
// #enddocregion
19+
@Component({
20+
selector: 'app-root',
21+
template: ` <h1>Component Two</h1> `,
22+
})
23+
export class ComponentTwo {
24+
}
25+
26+
@Component({
27+
selector: 'app-root',
28+
template: ` <h1>Component Three</h1> `,
29+
})
30+
export class ComponentThree {
31+
}
32+
33+
@Component({
34+
selector: 'app-root',
35+
template: ` <h1>Component Four</h1> `,
36+
})
37+
export class ComponentFour {
38+
}
39+
40+
@NgModule({imports: [BrowserModule], declarations: [ComponentOne, ComponentTwo]})
41+
export class AppModule implements DoBootstrap {
42+
// #docregion componentSelector
43+
ngDoBootstrap(appRef: ApplicationRef) {
44+
this.fetchDataFromApi().then((componentName: string) => {
45+
if (componentName === 'ComponentOne') {
46+
appRef.bootstrap(ComponentOne);
47+
} else {
48+
appRef.bootstrap(ComponentTwo);
49+
}
50+
});
51+
}
52+
// #enddocregion
53+
54+
fetchDataFromApi(): Promise<string> {
55+
return new Promise((resolve) => {
56+
setTimeout(() => {
57+
resolve('ComponentTwo');
58+
}, 2000);
59+
});
60+
}
61+
}
62+
63+
@NgModule({imports: [BrowserModule], declarations: [ComponentThree]})
64+
export class AppModuleTwo implements DoBootstrap {
65+
// #docregion cssSelector
66+
ngDoBootstrap(appRef: ApplicationRef) {
67+
appRef.bootstrap(ComponentThree, '#root-element');
68+
}
69+
// #enddocregion cssSelector
70+
}
71+
72+
@NgModule({imports: [BrowserModule], declarations: [ComponentFour]})
73+
export class AppModuleThree implements DoBootstrap {
74+
// #docregion domNode
75+
ngDoBootstrap(appRef: ApplicationRef) {
76+
const element = document.querySelector('#root-element');
77+
appRef.bootstrap(ComponentFour, element);
78+
}
79+
// #enddocregion domNode
80+
}

0 commit comments

Comments
 (0)