Skip to content

Commit 956b8c8

Browse files
committed
docs: bootstrap method
1 parent 3273ada commit 956b8c8

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

modules/angular2/src/core/annotations/annotations.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {ABSTRACT, CONST, normalizeBlank, isPresent} from 'angular2/src/facade/lang';
22
import {ListWrapper, List} from 'angular2/src/facade/collection';
33

4+
/**
5+
* Directives allow you to attach behavior to the DOM elements. Directive is an abstract concept, instead use concrete
6+
* directives such as: [Component], [Decorator] or [Viewport].
7+
*/
48
@ABSTRACT()
59
export class Directive {
610
selector:any; //string;

modules/angular2/src/core/application.js

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,113 @@ function _createVmZone(givenReporter:Function): VmTurnZone {
117117
return zone;
118118
}
119119

120-
// Multiple calls to this method are allowed. Each application would only share
121-
// _rootInjector, which is not user-configurable by design, thus safe to share.
122-
export function bootstrap(appComponentType: Type, bindings: List<Binding>=null, givenBootstrapErrorReporter: Function=null): Promise {
120+
/**
121+
* Bootstrapping for Angular applications.
122+
*
123+
* You instantiate an Angular application by explicitly specifying a component to use as the root component for your
124+
* application via the `bootstrap()` method.
125+
*
126+
* ## Simple Example
127+
*
128+
* Assuming this `index.html`:
129+
* <html>
130+
* <!-- load Angular script tags here. -->
131+
* <body>
132+
* <my-app>loading...</my-app>
133+
* </body>
134+
* </html>
135+
*
136+
* An application is bootstrapped inside an existing browser DOM, typically `index.html`. Unlike Angular 1, Angular 2
137+
* does not compile/process bindings in `index.html`. This is mainly for security reasons, as well as architectural
138+
* changes in Angular 2. This means that `index.html` can safely be processed using server-side binding technologies,
139+
* which may use double-curly `{{syntax}}` without collision from Angular 2 component double-curly `{{syntax}}`.
140+
*
141+
* We can use this script code:
142+
* @Component({
143+
* selector: 'my-app'
144+
* })
145+
* @Template({
146+
* inline: 'Hello {{name}}!'
147+
* })
148+
* class MyApp {
149+
* name:string;
150+
*
151+
* constructor() {
152+
* this.name = 'World';
153+
* }
154+
* }
155+
*
156+
* main() {
157+
* bootstrap(MyApp);
158+
* }
159+
*
160+
* When the app developer invokes `bootstrap()` with the root component `MyApp` as its argument, Angular performs the
161+
* following tasks:
162+
*
163+
* 1. It uses the component's `selector` property to locate the DOM element which needs to be upgraded into
164+
* the angular component.
165+
* 2. It creates a new child injector (from the primordial injector) and configures the injector with the component's
166+
* `services`. Optionally, you can also override the injector configuration for an app by invoking
167+
* `bootstrap` with the `componentServiceBindings` argument.
168+
* 3. It creates a new [Zone] and connects it to the angular application's change detection domain instance.
169+
* 4. It creates a shadow DOM on the selected component's host element and loads the template into it.
170+
* 5. It instantiates the specified component.
171+
* 6. Finally, Angular performs change detection to apply the initial data bindings for the application.
172+
*
173+
*
174+
* ## Instantiating Multiple Applications on a Single Page
175+
*
176+
* There are two ways to do this.
177+
*
178+
*
179+
* ### Isolated Applications
180+
*
181+
* Angular creates a new application each time that the `bootstrap()` method is invoked. When multiple applications
182+
* are created for a page, Angular treats each application as independent within an isolated change detection and
183+
* [Zone] domain. If you need to share data between applications, use the strategy described in the next
184+
* section, "Applications That Share Change Detection."
185+
*
186+
*
187+
* ### Applications That Share Change Detection
188+
*
189+
* If you need to bootstrap multiple applications that share common data, the applications must share a common
190+
* change detection and zone. To do that, create a meta-component that lists the application components in its template.
191+
* By only invoking the bootstrap()` method once with the meta-component as its argument, you ensure that only a single
192+
* change detection zone is created and therefore data can be shared across the applications.
193+
*
194+
*
195+
* ## Primordial Injector
196+
*
197+
* When working within a browser window, there are many singleton resources: cookies, title, location, and others.
198+
* Angular services that represent these resources must likewise be shared across all Angular applications that
199+
* occupy the same browser window. For this reason, Angular creates exactly one global primordial injector which stores
200+
* all shared services, and each angular application injector has the primordial injector as its parent.
201+
*
202+
* Each application has its own private injector as well. When there are multiple applications on a page, Angular treats
203+
* each application injector's services as private to that application.
204+
*
205+
*
206+
* # API
207+
* - [appComponentType]: The root component which should act as the application. This is a reference to a [Type]
208+
* which is annotated with `@Component(...)`.
209+
* - [componentServiceBindings]: An additional set of bindings that can be added to the [Component.services] to
210+
* override default injection behavior.
211+
* - [errorReporter]: `function(exception:any, stackTrace:string)` a default error reporter for unhandled exceptions.
212+
*
213+
* Returns the application`s private [Injector].
214+
*/
215+
export function bootstrap(appComponentType: Type,
216+
componentServiceBindings: List<Binding>=null,
217+
errorReporter: Function=null): Promise<Injector> {
123218
BrowserDomAdapter.makeCurrent();
124219
var bootstrapProcess = PromiseWrapper.completer();
125220

126-
var zone = _createVmZone(givenBootstrapErrorReporter);
221+
var zone = _createVmZone(errorReporter);
127222
zone.run(() => {
128223
// TODO(rado): prepopulate template cache, so applications with only
129224
// index.html and main.js are possible.
130225

131-
var appInjector = _createAppInjector(appComponentType, bindings, zone);
226+
var appInjector = _createAppInjector(appComponentType, componentServiceBindings, zone);
132227

133228
PromiseWrapper.then(appInjector.asyncGet(appViewToken),
134229
(rootView) => {

0 commit comments

Comments
 (0)