2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
import {ObservableWrapper, PromiseWrapper} from '../src/facade/async';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {ListWrapper} from '../src/facade/collection';
|
2016-05-02 01:50:37 -04:00
|
|
|
import {BaseException, ExceptionHandler, unimplemented} from '../src/facade/exceptions';
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
import {ConcreteType, IS_DART, Type, isBlank, isPresent, isPromise} from '../src/facade/lang';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
|
|
|
import {APP_INITIALIZER, PLATFORM_INITIALIZER} from './application_tokens';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {ChangeDetectorRef} from './change_detection/change_detector_ref';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Console} from './console';
|
2016-07-18 06:50:31 -04:00
|
|
|
import {Inject, Injectable, Injector, OpaqueToken, Optional, ReflectiveInjector, SkipSelf, forwardRef} from './di';
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
import {Compiler, CompilerFactory, CompilerOptions} from './linker/compiler';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {ComponentFactory, ComponentRef} from './linker/component_factory';
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
import {ComponentFactoryResolver} from './linker/component_factory_resolver';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {ComponentResolver} from './linker/component_resolver';
|
2016-07-18 06:50:31 -04:00
|
|
|
import {NgModuleFactory, NgModuleRef} from './linker/ng_module_factory';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {WtfScopeFn, wtfCreateScope, wtfLeave} from './profile/profile';
|
|
|
|
import {Testability, TestabilityRegistry} from './testability/testability';
|
|
|
|
import {NgZone, NgZoneError} from './zone/ng_zone';
|
|
|
|
|
2016-06-17 17:09:19 -04:00
|
|
|
var _devMode: boolean = true;
|
|
|
|
var _runModeLocked: boolean = false;
|
2015-09-02 18:19:26 -04:00
|
|
|
var _platform: PlatformRef;
|
2016-04-14 17:52:35 -04:00
|
|
|
var _inPlatformCreate: boolean = false;
|
2015-09-02 18:19:26 -04:00
|
|
|
|
2016-06-17 17:09:19 -04:00
|
|
|
/**
|
|
|
|
* Disable Angular's development mode, which turns off assertions and other
|
|
|
|
* checks within the framework.
|
|
|
|
*
|
|
|
|
* One important assertion this disables verifies that a change detection pass
|
|
|
|
* does not result in additional changes to any bindings (also known as
|
|
|
|
* unidirectional data flow).
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
2016-06-17 17:09:19 -04:00
|
|
|
*/
|
|
|
|
export function enableProdMode(): void {
|
|
|
|
if (_runModeLocked) {
|
|
|
|
// Cannot use BaseException as that ends up importing from facade/lang.
|
|
|
|
throw new BaseException('Cannot enable prod mode after platform setup.');
|
|
|
|
}
|
|
|
|
_devMode = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-07 17:42:46 -04:00
|
|
|
* Locks the run mode of Angular. After this has been called,
|
|
|
|
* it can't be changed any more. I.e. `isDevMode()` will always
|
|
|
|
* return the same value.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-07-07 17:42:46 -04:00
|
|
|
* @deprecated This is a noop now. {@link isDevMode} automatically locks the run mode on first call.
|
2016-06-17 17:09:19 -04:00
|
|
|
*/
|
2016-07-07 17:42:46 -04:00
|
|
|
export function lockRunMode(): void {
|
|
|
|
console.warn('lockRunMode() is deprecated and not needed any more.');
|
2016-06-17 17:09:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-07 17:42:46 -04:00
|
|
|
* Returns whether Angular is in development mode. After called once,
|
|
|
|
* the value is locked and won't change any more.
|
|
|
|
*
|
|
|
|
* By default, this is true, unless a user calls `enableProdMode` before calling this.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
2016-06-17 17:09:19 -04:00
|
|
|
*/
|
2016-07-07 17:42:46 -04:00
|
|
|
export function isDevMode(): boolean {
|
2016-06-17 17:09:19 -04:00
|
|
|
_runModeLocked = true;
|
2016-07-07 17:42:46 -04:00
|
|
|
return _devMode;
|
2016-06-17 17:09:19 -04:00
|
|
|
}
|
|
|
|
|
2015-11-13 14:21:16 -05:00
|
|
|
/**
|
2016-04-14 17:52:35 -04:00
|
|
|
* Creates a platform.
|
|
|
|
* Platforms have to be eagerly created via this function.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
2015-11-13 14:21:16 -05:00
|
|
|
*/
|
2016-04-14 17:52:35 -04:00
|
|
|
export function createPlatform(injector: Injector): PlatformRef {
|
|
|
|
if (_inPlatformCreate) {
|
|
|
|
throw new BaseException('Already creating a platform...');
|
|
|
|
}
|
|
|
|
if (isPresent(_platform) && !_platform.disposed) {
|
|
|
|
throw new BaseException(
|
2016-06-08 19:38:52 -04:00
|
|
|
'There can be only one platform. Destroy the previous one to create a new one.');
|
2016-04-14 17:52:35 -04:00
|
|
|
}
|
|
|
|
_inPlatformCreate = true;
|
|
|
|
try {
|
|
|
|
_platform = injector.get(PlatformRef);
|
|
|
|
} finally {
|
|
|
|
_inPlatformCreate = false;
|
2015-11-12 16:40:29 -05:00
|
|
|
}
|
2016-04-14 17:52:35 -04:00
|
|
|
return _platform;
|
|
|
|
}
|
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
/**
|
|
|
|
* Factory for a platform.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export type PlatformFactory = (extraProviders?: any[]) => PlatformRef;
|
|
|
|
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
/**
|
|
|
|
* Creates a fatory for a platform
|
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
|
|
|
*/
|
2016-07-18 06:50:31 -04:00
|
|
|
export function createPlatformFactory(
|
|
|
|
parentPlaformFactory: PlatformFactory, name: string, providers: any[] = []): PlatformFactory {
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
const marker = new OpaqueToken(`Platform: ${name}`);
|
2016-07-18 06:50:31 -04:00
|
|
|
return (extraProviders: any[] = []) => {
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
if (!getPlatform()) {
|
2016-07-18 06:50:31 -04:00
|
|
|
if (parentPlaformFactory) {
|
|
|
|
parentPlaformFactory(
|
|
|
|
providers.concat(extraProviders).concat({provide: marker, useValue: true}));
|
|
|
|
} else {
|
|
|
|
createPlatform(ReflectiveInjector.resolveAndCreate(
|
|
|
|
providers.concat(extraProviders).concat({provide: marker, useValue: true})));
|
|
|
|
}
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
}
|
|
|
|
return assertPlatform(marker);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
/**
|
|
|
|
* Checks that there currently is a platform
|
|
|
|
* which contains the given token as a provider.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
2016-04-14 17:52:35 -04:00
|
|
|
*/
|
|
|
|
export function assertPlatform(requiredToken: any): PlatformRef {
|
|
|
|
var platform = getPlatform();
|
|
|
|
if (isBlank(platform)) {
|
2016-05-20 14:18:08 -04:00
|
|
|
throw new BaseException('No platform exists!');
|
2016-04-14 17:52:35 -04:00
|
|
|
}
|
|
|
|
if (isPresent(platform) && isBlank(platform.injector.get(requiredToken, null))) {
|
|
|
|
throw new BaseException(
|
|
|
|
'A platform with a different configuration has been created. Please destroy it first.');
|
|
|
|
}
|
|
|
|
return platform;
|
2015-11-13 14:21:16 -05:00
|
|
|
}
|
2015-11-12 16:40:29 -05:00
|
|
|
|
2015-11-18 12:18:37 -05:00
|
|
|
/**
|
|
|
|
* Dispose the existing platform.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
2015-11-18 12:18:37 -05:00
|
|
|
*/
|
|
|
|
export function disposePlatform(): void {
|
2016-04-14 17:52:35 -04:00
|
|
|
if (isPresent(_platform) && !_platform.disposed) {
|
2015-11-18 12:18:37 -05:00
|
|
|
_platform.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
/**
|
|
|
|
* Returns the current platform.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
2016-04-14 17:52:35 -04:00
|
|
|
*/
|
|
|
|
export function getPlatform(): PlatformRef {
|
|
|
|
return isPresent(_platform) && !_platform.disposed ? _platform : null;
|
2015-09-02 18:19:26 -04:00
|
|
|
}
|
|
|
|
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
/**
|
2016-07-18 06:50:31 -04:00
|
|
|
* Creates an instance of an `@NgModule` for the given platform
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
* for offline compilation.
|
|
|
|
*
|
|
|
|
* ## Simple Example
|
|
|
|
*
|
|
|
|
* ```typescript
|
|
|
|
* my_module.ts:
|
|
|
|
*
|
2016-07-18 06:50:31 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [BrowserModule]
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
* })
|
|
|
|
* class MyModule {}
|
|
|
|
*
|
|
|
|
* main.ts:
|
|
|
|
* import {MyModuleNgFactory} from './my_module.ngfactory';
|
|
|
|
* import {bootstrapModuleFactory} from '@angular/core';
|
|
|
|
* import {browserPlatform} from '@angular/platform-browser';
|
|
|
|
*
|
|
|
|
* let moduleRef = bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
|
|
|
*/
|
|
|
|
export function bootstrapModuleFactory<M>(
|
2016-07-18 06:50:31 -04:00
|
|
|
moduleFactory: NgModuleFactory<M>, platform: PlatformRef): NgModuleRef<M> {
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
// Note: We need to create the NgZone _before_ we instantiate the module,
|
|
|
|
// as instantiating the module creates some providers eagerly.
|
|
|
|
// So we create a mini parent injector that just contains the new NgZone and
|
2016-07-18 06:50:31 -04:00
|
|
|
// pass that as parent to the NgModuleFactory.
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
const ngZone = new NgZone({enableLongStackTrace: isDevMode()});
|
|
|
|
const ngZoneInjector =
|
|
|
|
ReflectiveInjector.resolveAndCreate([{provide: NgZone, useValue: ngZone}], platform.injector);
|
|
|
|
return ngZone.run(() => moduleFactory.create(ngZoneInjector));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-18 06:50:31 -04:00
|
|
|
* Creates an instance of an `@NgModule` for a given platform using the given runtime compiler.
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
*
|
|
|
|
* ## Simple Example
|
|
|
|
*
|
|
|
|
* ```typescript
|
2016-07-18 06:50:31 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [BrowserModule]
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
* })
|
|
|
|
* class MyModule {}
|
|
|
|
*
|
|
|
|
* let moduleRef = bootstrapModule(MyModule, browserPlatform());
|
|
|
|
* ```
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export function bootstrapModule<M>(
|
|
|
|
moduleType: ConcreteType<M>, platform: PlatformRef,
|
2016-07-18 06:50:31 -04:00
|
|
|
compilerOptions: CompilerOptions | CompilerOptions[] = []): Promise<NgModuleRef<M>> {
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
const compilerFactory: CompilerFactory = platform.injector.get(CompilerFactory);
|
2016-07-18 06:50:31 -04:00
|
|
|
const compiler = compilerFactory.createCompiler(
|
|
|
|
compilerOptions instanceof Array ? compilerOptions : [compilerOptions]);
|
|
|
|
return compiler.compileModuleAsync(moduleType)
|
refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`.
- Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule`
- Introduces `serverDynamicPlatform` for applications using runtime compilation
on the server.
- Changes browser bootstrap for runtime and offline compilation (see below for an example).
* introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core`
* introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic
- Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example).
BREAKING CHANGE:
## Migration from `setBaseTestProviders` to `initTestEnvironment`:
- For the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
BrowserDynamicTestModule,
browserDynamicTestPlatform());
```
- For the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {initTestEnvironment} from ‘@angular/core/testing’;
import {serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
ServerTestModule,
serverTestPlatform());
```
## Bootstrap changes
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {browserPlatform} from ‘@angular/platform-browser’;
import {bootstrapModuleFactory} from ‘@angular/core’;
bootstrapModuleFactory(MyModuleNgFactory, browserPlatform());
// runtime compile long form
import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’;
import {bootstrapModule} from ‘@angular/core’;
bootstrapModule(MyModule, browserDynamicPlatform());
```
Closes #9922
Part of #9726
2016-07-08 13:47:17 -04:00
|
|
|
.then((moduleFactory) => bootstrapModuleFactory(moduleFactory, platform))
|
|
|
|
.then((moduleRef) => {
|
|
|
|
const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
|
|
|
return appRef.waitForAsyncInitializers().then(() => moduleRef);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
/**
|
|
|
|
* Shortcut for ApplicationRef.bootstrap.
|
2016-05-20 14:18:08 -04:00
|
|
|
* Requires a platform to be created first.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-07-08 16:40:54 -04:00
|
|
|
* @deprecated Use {@link bootstrapModuleFactory} instead.
|
2016-04-14 17:52:35 -04:00
|
|
|
*/
|
2016-06-08 19:38:52 -04:00
|
|
|
export function coreBootstrap<C>(
|
|
|
|
componentFactory: ComponentFactory<C>, injector: Injector): ComponentRef<C> {
|
2016-07-18 06:50:31 -04:00
|
|
|
throw new BaseException('coreBootstrap is deprecated. Use bootstrapModuleFactory instead.');
|
2016-04-14 17:52:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the componentFactory for the given component,
|
|
|
|
* waits for asynchronous initializers and bootstraps the component.
|
2016-05-20 14:18:08 -04:00
|
|
|
* Requires a platform to be created first.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-07-08 16:40:54 -04:00
|
|
|
* @deprecated Use {@link bootstrapModule} instead.
|
2016-04-14 17:52:35 -04:00
|
|
|
*/
|
2016-06-08 19:38:52 -04:00
|
|
|
export function coreLoadAndBootstrap(
|
|
|
|
componentType: Type, injector: Injector): Promise<ComponentRef<any>> {
|
2016-07-18 06:50:31 -04:00
|
|
|
throw new BaseException('coreLoadAndBootstrap is deprecated. Use bootstrapModule instead.');
|
2015-11-18 12:18:37 -05:00
|
|
|
}
|
|
|
|
|
2015-09-02 18:19:26 -04:00
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* The Angular platform is the entry point for Angular on a web page. Each page
|
|
|
|
* has exactly one platform, and services (such as reflection) which are common
|
|
|
|
* to every Angular application running on the page are bound in its scope.
|
2015-08-20 20:18:27 -04:00
|
|
|
*
|
2015-09-15 18:51:15 -04:00
|
|
|
* A page's platform is initialized implicitly when {@link bootstrap}() is called, or
|
2016-04-14 17:52:35 -04:00
|
|
|
* explicitly by calling {@link createPlatform}().
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
2015-08-20 20:18:27 -04:00
|
|
|
*/
|
2015-10-06 09:53:39 -04:00
|
|
|
export abstract class PlatformRef {
|
2015-10-26 13:50:25 -04:00
|
|
|
/**
|
|
|
|
* Register a listener to be called when the platform is disposed.
|
|
|
|
*/
|
|
|
|
abstract registerDisposeListener(dispose: () => void): void;
|
|
|
|
|
2015-09-02 18:19:26 -04:00
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* Retrieve the platform {@link Injector}, which is the parent injector for
|
2015-10-11 01:11:13 -04:00
|
|
|
* every Angular application on the page and provides singleton providers.
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2016-02-19 14:49:31 -05:00
|
|
|
get injector(): Injector { throw unimplemented(); };
|
2015-08-20 20:18:27 -04:00
|
|
|
|
2015-10-06 09:53:39 -04:00
|
|
|
/**
|
|
|
|
* Destroy the Angular platform and all Angular applications on the page.
|
|
|
|
*/
|
|
|
|
abstract dispose(): void;
|
2016-04-14 17:52:35 -04:00
|
|
|
|
|
|
|
get disposed(): boolean { throw unimplemented(); }
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
@Injectable()
|
2015-10-06 09:53:39 -04:00
|
|
|
export class PlatformRef_ extends PlatformRef {
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-10-06 09:53:39 -04:00
|
|
|
_applications: ApplicationRef[] = [];
|
2015-10-28 17:16:54 -04:00
|
|
|
/** @internal */
|
2015-10-26 13:50:25 -04:00
|
|
|
_disposeListeners: Function[] = [];
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
private _disposed: boolean = false;
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
constructor(private _injector: Injector) {
|
|
|
|
super();
|
|
|
|
if (!_inPlatformCreate) {
|
|
|
|
throw new BaseException('Platforms have to be created via `createPlatform`!');
|
2015-12-20 18:10:36 -05:00
|
|
|
}
|
2016-04-14 17:52:35 -04:00
|
|
|
let inits: Function[] = <Function[]>_injector.get(PLATFORM_INITIALIZER, null);
|
|
|
|
if (isPresent(inits)) inits.forEach(init => init());
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); }
|
2015-09-02 18:19:26 -04:00
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
get injector(): Injector { return this._injector; }
|
2015-09-02 18:19:26 -04:00
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
get disposed() { return this._disposed; }
|
|
|
|
|
|
|
|
addApplication(appRef: ApplicationRef) { this._applications.push(appRef); }
|
2015-09-02 18:19:26 -04:00
|
|
|
|
2015-08-20 20:18:27 -04:00
|
|
|
dispose(): void {
|
2015-11-18 12:18:37 -05:00
|
|
|
ListWrapper.clone(this._applications).forEach((app) => app.dispose());
|
2015-10-26 13:50:25 -04:00
|
|
|
this._disposeListeners.forEach((dispose) => dispose());
|
2016-04-14 17:52:35 -04:00
|
|
|
this._disposed = true;
|
2015-09-02 18:19:26 -04:00
|
|
|
}
|
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-02 18:19:26 -04:00
|
|
|
_applicationDisposed(app: ApplicationRef): void { ListWrapper.remove(this._applications, app); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* A reference to an Angular application running on a page.
|
2015-09-02 18:19:26 -04:00
|
|
|
*
|
2015-09-15 18:51:15 -04:00
|
|
|
* For more about Angular applications, see the documentation for {@link bootstrap}.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental APIs related to application bootstrap are currently under review.
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2015-10-06 09:53:39 -04:00
|
|
|
export abstract class ApplicationRef {
|
2015-09-02 18:19:26 -04:00
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* Register a listener to be called each time `bootstrap()` is called to bootstrap
|
|
|
|
* a new root component.
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2016-04-30 13:52:04 -04:00
|
|
|
abstract registerBootstrapListener(listener: (ref: ComponentRef<any>) => void): void;
|
2015-08-20 20:18:27 -04:00
|
|
|
|
2015-10-26 13:50:25 -04:00
|
|
|
/**
|
|
|
|
* Register a listener to be called when the application is disposed.
|
|
|
|
*/
|
|
|
|
abstract registerDisposeListener(dispose: () => void): void;
|
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
/**
|
|
|
|
* Returns a promise that resolves when all asynchronous application initializers
|
|
|
|
* are done.
|
|
|
|
*/
|
|
|
|
abstract waitForAsyncInitializers(): Promise<any>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the given callback in the zone and returns the result of the callback.
|
|
|
|
* Exceptions will be forwarded to the ExceptionHandler and rethrown.
|
|
|
|
*/
|
|
|
|
abstract run(callback: Function): any;
|
|
|
|
|
2015-08-20 20:18:27 -04:00
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* Bootstrap a new component at the root level of the application.
|
|
|
|
*
|
2015-11-17 12:41:31 -05:00
|
|
|
* ### Bootstrap process
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
|
|
|
* When bootstrapping a new root component into an application, Angular mounts the
|
|
|
|
* specified application component onto DOM elements identified by the [componentType]'s
|
|
|
|
* selector and kicks off automatic change detection to finish initializing the component.
|
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
* ### Example
|
2015-11-30 11:28:54 -05:00
|
|
|
* {@example core/ts/platform/platform.ts region='longform'}
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
abstract bootstrap<C>(componentFactory: ComponentFactory<C>|ConcreteType<C>): ComponentRef<C>;
|
2015-10-06 09:53:39 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the application {@link Injector}.
|
|
|
|
*/
|
2016-02-19 14:49:31 -05:00
|
|
|
get injector(): Injector { return <Injector>unimplemented(); };
|
2015-10-06 09:53:39 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the application {@link NgZone}.
|
|
|
|
*/
|
2016-02-19 14:49:31 -05:00
|
|
|
get zone(): NgZone { return <NgZone>unimplemented(); };
|
2015-10-06 09:53:39 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispose of this application and all of its components.
|
|
|
|
*/
|
|
|
|
abstract dispose(): void;
|
2015-10-09 19:22:07 -04:00
|
|
|
|
2015-10-28 13:34:13 -04:00
|
|
|
/**
|
|
|
|
* Invoke this method to explicitly process change detection and its side-effects.
|
|
|
|
*
|
|
|
|
* In development mode, `tick()` also performs a second change detection cycle to ensure that no
|
|
|
|
* further changes are detected. If additional changes are picked up during this second cycle,
|
|
|
|
* bindings in the app have side-effects that cannot be resolved in a single change detection
|
|
|
|
* pass.
|
|
|
|
* In this case, Angular throws an error, since an Angular application can only have one change
|
|
|
|
* detection pass during which all change detection must complete.
|
|
|
|
*/
|
|
|
|
abstract tick(): void;
|
|
|
|
|
2015-10-09 19:22:07 -04:00
|
|
|
/**
|
|
|
|
* Get a list of component types registered to this application.
|
|
|
|
*/
|
2016-02-19 14:49:31 -05:00
|
|
|
get componentTypes(): Type[] { return <Type[]>unimplemented(); };
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
@Injectable()
|
2015-10-06 09:53:39 -04:00
|
|
|
export class ApplicationRef_ extends ApplicationRef {
|
2015-10-28 13:34:13 -04:00
|
|
|
/** @internal */
|
|
|
|
static _tickScope: WtfScopeFn = wtfCreateScope('ApplicationRef#tick()');
|
|
|
|
|
|
|
|
/** @internal */
|
2015-10-06 09:53:39 -04:00
|
|
|
private _bootstrapListeners: Function[] = [];
|
2015-10-28 13:34:13 -04:00
|
|
|
/** @internal */
|
2015-10-26 13:50:25 -04:00
|
|
|
private _disposeListeners: Function[] = [];
|
2015-10-28 13:34:13 -04:00
|
|
|
/** @internal */
|
2016-04-30 13:52:04 -04:00
|
|
|
private _rootComponents: ComponentRef<any>[] = [];
|
2015-10-28 13:34:13 -04:00
|
|
|
/** @internal */
|
2015-10-09 19:22:07 -04:00
|
|
|
private _rootComponentTypes: Type[] = [];
|
2015-10-28 13:34:13 -04:00
|
|
|
/** @internal */
|
|
|
|
private _changeDetectorRefs: ChangeDetectorRef[] = [];
|
|
|
|
/** @internal */
|
|
|
|
private _runningTick: boolean = false;
|
|
|
|
/** @internal */
|
|
|
|
private _enforceNoNewChanges: boolean = false;
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
private _asyncInitDonePromise: Promise<any>;
|
|
|
|
private _asyncInitDone: boolean;
|
|
|
|
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
constructor(
|
|
|
|
private _platform: PlatformRef_, private _zone: NgZone, private _console: Console,
|
|
|
|
private _injector: Injector, private _exceptionHandler: ExceptionHandler,
|
|
|
|
private _componentFactoryResolver: ComponentFactoryResolver,
|
|
|
|
@Optional() private _testabilityRegistry: TestabilityRegistry,
|
|
|
|
@Optional() private _testability: Testability,
|
|
|
|
@Optional() @Inject(APP_INITIALIZER) inits: Function[]) {
|
2015-10-06 09:53:39 -04:00
|
|
|
super();
|
2016-06-17 17:09:19 -04:00
|
|
|
this._enforceNoNewChanges = isDevMode();
|
2016-04-14 17:52:35 -04:00
|
|
|
this._asyncInitDonePromise = this.run(() => {
|
2016-06-28 14:35:59 -04:00
|
|
|
var asyncInitResults: Promise<any>[] = [];
|
|
|
|
var asyncInitDonePromise: Promise<any>;
|
2016-04-14 17:52:35 -04:00
|
|
|
if (isPresent(inits)) {
|
|
|
|
for (var i = 0; i < inits.length; i++) {
|
|
|
|
var initResult = inits[i]();
|
|
|
|
if (isPromise(initResult)) {
|
|
|
|
asyncInitResults.push(initResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (asyncInitResults.length > 0) {
|
|
|
|
asyncInitDonePromise =
|
|
|
|
PromiseWrapper.all(asyncInitResults).then((_) => this._asyncInitDone = true);
|
|
|
|
this._asyncInitDone = false;
|
|
|
|
} else {
|
|
|
|
this._asyncInitDone = true;
|
|
|
|
asyncInitDonePromise = PromiseWrapper.resolve(true);
|
|
|
|
}
|
|
|
|
return asyncInitDonePromise;
|
|
|
|
});
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
ObservableWrapper.subscribe(this._zone.onError, (error: NgZoneError) => {
|
2016-04-14 17:52:35 -04:00
|
|
|
this._exceptionHandler.call(error.error, error.stackTrace);
|
|
|
|
});
|
2016-06-08 19:38:52 -04:00
|
|
|
ObservableWrapper.subscribe(
|
|
|
|
this._zone.onMicrotaskEmpty, (_) => { this._zone.run(() => { this.tick(); }); });
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-04-30 13:52:04 -04:00
|
|
|
registerBootstrapListener(listener: (ref: ComponentRef<any>) => void): void {
|
2015-10-06 09:53:39 -04:00
|
|
|
this._bootstrapListeners.push(listener);
|
|
|
|
}
|
|
|
|
|
2015-10-26 13:50:25 -04:00
|
|
|
registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); }
|
|
|
|
|
2015-10-28 13:34:13 -04:00
|
|
|
registerChangeDetector(changeDetector: ChangeDetectorRef): void {
|
|
|
|
this._changeDetectorRefs.push(changeDetector);
|
|
|
|
}
|
|
|
|
|
2015-11-10 13:40:33 -05:00
|
|
|
unregisterChangeDetector(changeDetector: ChangeDetectorRef): void {
|
|
|
|
ListWrapper.remove(this._changeDetectorRefs, changeDetector);
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:52:35 -04:00
|
|
|
waitForAsyncInitializers(): Promise<any> { return this._asyncInitDonePromise; }
|
|
|
|
|
|
|
|
run(callback: Function): any {
|
2016-06-28 14:35:59 -04:00
|
|
|
var result: any;
|
2016-04-14 17:52:35 -04:00
|
|
|
// Note: Don't use zone.runGuarded as we want to know about
|
|
|
|
// the thrown exception!
|
|
|
|
// Note: the completer needs to be created outside
|
|
|
|
// of `zone.run` as Dart swallows rejected promises
|
|
|
|
// via the onError callback of the promise.
|
2015-09-02 18:19:26 -04:00
|
|
|
var completer = PromiseWrapper.completer();
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
this._zone.run(() => {
|
2015-09-02 18:19:26 -04:00
|
|
|
try {
|
2016-04-14 17:52:35 -04:00
|
|
|
result = callback();
|
|
|
|
if (isPromise(result)) {
|
2016-06-08 19:38:52 -04:00
|
|
|
PromiseWrapper.then(
|
|
|
|
result, (ref) => { completer.resolve(ref); },
|
|
|
|
(err, stackTrace) => {
|
|
|
|
completer.reject(err, stackTrace);
|
|
|
|
this._exceptionHandler.call(err, stackTrace);
|
|
|
|
});
|
2016-04-14 17:52:35 -04:00
|
|
|
}
|
2015-09-02 18:19:26 -04:00
|
|
|
} catch (e) {
|
2016-04-14 17:52:35 -04:00
|
|
|
this._exceptionHandler.call(e, e.stack);
|
|
|
|
throw e;
|
2015-09-02 18:19:26 -04:00
|
|
|
}
|
|
|
|
});
|
2016-04-14 17:52:35 -04:00
|
|
|
return isPromise(result) ? completer.promise : result;
|
|
|
|
}
|
|
|
|
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
bootstrap<C>(componentOrFactory: ComponentFactory<C>|ConcreteType<C>): ComponentRef<C> {
|
2016-04-14 17:52:35 -04:00
|
|
|
if (!this._asyncInitDone) {
|
|
|
|
throw new BaseException(
|
|
|
|
'Cannot bootstrap as there are still asynchronous initializers running. Wait for them using waitForAsyncInitializers().');
|
|
|
|
}
|
|
|
|
return this.run(() => {
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
let componentFactory: ComponentFactory<C>;
|
|
|
|
if (componentOrFactory instanceof ComponentFactory) {
|
|
|
|
componentFactory = componentOrFactory;
|
|
|
|
} else {
|
|
|
|
componentFactory =
|
|
|
|
this._componentFactoryResolver.resolveComponentFactory(componentOrFactory);
|
|
|
|
}
|
2016-04-14 17:52:35 -04:00
|
|
|
this._rootComponentTypes.push(componentFactory.componentType);
|
|
|
|
var compRef = componentFactory.create(this._injector, [], componentFactory.selector);
|
|
|
|
compRef.onDestroy(() => { this._unloadComponent(compRef); });
|
|
|
|
var testability = compRef.injector.get(Testability, null);
|
|
|
|
if (isPresent(testability)) {
|
|
|
|
compRef.injector.get(TestabilityRegistry)
|
|
|
|
.registerApplication(compRef.location.nativeElement, testability);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._loadComponent(compRef);
|
2016-06-17 17:09:19 -04:00
|
|
|
if (isDevMode()) {
|
2016-06-08 19:38:52 -04:00
|
|
|
let prodDescription = IS_DART ? 'Production mode is disabled in Dart.' :
|
|
|
|
'Call enableProdMode() to enable the production mode.';
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
this._console.log(`Angular 2 is running in the development mode. ${prodDescription}`);
|
2016-02-03 14:47:53 -05:00
|
|
|
}
|
2016-04-14 17:52:35 -04:00
|
|
|
return compRef;
|
2015-12-15 11:34:44 -05:00
|
|
|
});
|
2015-09-02 18:19:26 -04:00
|
|
|
}
|
|
|
|
|
2015-11-10 13:40:33 -05:00
|
|
|
/** @internal */
|
2016-04-30 13:52:04 -04:00
|
|
|
_loadComponent(componentRef: ComponentRef<any>): void {
|
2016-04-13 20:05:17 -04:00
|
|
|
this._changeDetectorRefs.push(componentRef.changeDetectorRef);
|
2015-11-10 13:40:33 -05:00
|
|
|
this.tick();
|
2016-02-11 20:01:17 -05:00
|
|
|
this._rootComponents.push(componentRef);
|
|
|
|
this._bootstrapListeners.forEach((listener) => listener(componentRef));
|
2015-11-10 13:40:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2016-04-30 13:52:04 -04:00
|
|
|
_unloadComponent(componentRef: ComponentRef<any>): void {
|
2016-02-11 20:01:17 -05:00
|
|
|
if (!ListWrapper.contains(this._rootComponents, componentRef)) {
|
2015-11-10 13:40:33 -05:00
|
|
|
return;
|
|
|
|
}
|
2016-04-13 20:05:17 -04:00
|
|
|
this.unregisterChangeDetector(componentRef.changeDetectorRef);
|
2016-02-11 20:01:17 -05:00
|
|
|
ListWrapper.remove(this._rootComponents, componentRef);
|
2015-11-10 13:40:33 -05:00
|
|
|
}
|
|
|
|
|
2015-08-20 20:18:27 -04:00
|
|
|
get injector(): Injector { return this._injector; }
|
2015-09-02 18:19:26 -04:00
|
|
|
|
|
|
|
get zone(): NgZone { return this._zone; }
|
|
|
|
|
2015-10-28 13:34:13 -04:00
|
|
|
tick(): void {
|
|
|
|
if (this._runningTick) {
|
2016-06-08 19:38:52 -04:00
|
|
|
throw new BaseException('ApplicationRef.tick is called recursively');
|
2015-10-28 13:34:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var s = ApplicationRef_._tickScope();
|
|
|
|
try {
|
|
|
|
this._runningTick = true;
|
|
|
|
this._changeDetectorRefs.forEach((detector) => detector.detectChanges());
|
|
|
|
if (this._enforceNoNewChanges) {
|
|
|
|
this._changeDetectorRefs.forEach((detector) => detector.checkNoChanges());
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this._runningTick = false;
|
|
|
|
wtfLeave(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 18:19:26 -04:00
|
|
|
dispose(): void {
|
|
|
|
// TODO(alxhub): Dispose of the NgZone.
|
2016-04-13 20:05:17 -04:00
|
|
|
ListWrapper.clone(this._rootComponents).forEach((ref) => ref.destroy());
|
2015-10-26 13:50:25 -04:00
|
|
|
this._disposeListeners.forEach((dispose) => dispose());
|
2015-09-02 18:19:26 -04:00
|
|
|
this._platform._applicationDisposed(this);
|
|
|
|
}
|
2015-10-09 19:22:07 -04:00
|
|
|
|
2016-01-21 21:13:58 -05:00
|
|
|
get componentTypes(): Type[] { return this._rootComponentTypes; }
|
2015-08-20 20:18:27 -04:00
|
|
|
}
|