-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,14 @@ | |
import {ComponentFactory, ComponentFactoryResolver, Injector, Type} from '@angular/core'; | ||
|
||
import * as angular from './angular1'; | ||
import {$COMPILE, $INJECTOR, $PARSE, INJECTOR_KEY, REQUIRE_INJECTOR, REQUIRE_NG_MODEL} from './constants'; | ||
import {$COMPILE, $INJECTOR, $PARSE, INJECTOR_KEY, LAZY_MODULE_REF, REQUIRE_INJECTOR, REQUIRE_NG_MODEL} from './constants'; | ||
import {DowngradeComponentAdapter} from './downgrade_component_adapter'; | ||
import {controllerKey, getComponentName} from './util'; | ||
import {LazyModuleRef, controllerKey, getComponentName, isFunction} from './util'; | ||
|
||
|
||
interface Thenable<T> { | ||
then(callback: (value: T) => any): any; | ||
} | ||
|
||
let downgradeCount = 0; | ||
|
||
|
@@ -50,6 +55,8 @@ let downgradeCount = 0; | |
*/ | ||
export function downgradeComponent(info: { | ||
component: Type<any>; | ||
/** @experimental */ | ||
propagateDigest?: boolean; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
KhalipskiSiarhei
|
||
/** @deprecated since v4. This parameter is no longer used */ | ||
inputs?: string[]; | ||
/** @deprecated since v4. This parameter is no longer used */ | ||
|
@@ -76,9 +83,14 @@ export function downgradeComponent(info: { | |
// triggered by `UpgradeNg1ComponentAdapterBuilder`, before the Angular templates have | ||
// been compiled. | ||
|
||
const parentInjector: Injector|ParentInjectorPromise = | ||
required[0] || $injector.get(INJECTOR_KEY); | ||
const ngModel: angular.INgModelController = required[1]; | ||
let parentInjector: Injector|Thenable<Injector>|undefined = required[0]; | ||
let ranAsync = false; | ||
|
||
if (!parentInjector) { | ||
const lazyModuleRef = $injector.get(LAZY_MODULE_REF) as LazyModuleRef; | ||
parentInjector = lazyModuleRef.injector || lazyModuleRef.promise; | ||
} | ||
|
||
const downgradeFn = (injector: Injector) => { | ||
const componentFactoryResolver: ComponentFactoryResolver = | ||
|
@@ -98,18 +110,26 @@ export function downgradeComponent(info: { | |
|
||
const projectableNodes = facade.compileContents(); | ||
facade.createComponent(projectableNodes); | ||
facade.setupInputs(); | ||
facade.setupInputs(info.propagateDigest); | ||
facade.setupOutputs(); | ||
facade.registerCleanup(); | ||
|
||
injectorPromise.resolve(facade.getInjector()); | ||
|
||
if (ranAsync) { | ||
// If this is run async, it is possible that it is not run inside a | ||
// digest and initial input values will not be detected. | ||
scope.$evalAsync(() => {}); | ||
} | ||
}; | ||
|
||
if (parentInjector instanceof ParentInjectorPromise) { | ||
if (isThenable<Injector>(parentInjector)) { | ||
parentInjector.then(downgradeFn); | ||
} else { | ||
downgradeFn(parentInjector); | ||
} | ||
|
||
ranAsync = true; | ||
} | ||
}; | ||
}; | ||
|
@@ -155,3 +175,7 @@ class ParentInjectorPromise { | |
this.callbacks.length = 0; | ||
} | ||
} | ||
|
||
function isThenable<T>(obj: object): obj is Thenable<T> { | ||
return isFunction((obj as any).then); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @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 | ||
*/ | ||
|
||
import {Injector, NgModuleFactory, NgModuleRef, Provider} from '@angular/core'; | ||
import {platformBrowser} from '@angular/platform-browser'; | ||
|
||
import * as angular from '../common/angular1'; | ||
import {$INJECTOR, INJECTOR_KEY, LAZY_MODULE_REF, UPGRADE_MODULE_NAME} from '../common/constants'; | ||
import {LazyModuleRef, isFunction} from '../common/util'; | ||
|
||
import {angular1Providers, setTempInjectorRef} from './angular1_providers'; | ||
import {NgAdapterInjector} from './util'; | ||
|
||
|
||
/** @experimental */ | ||
export function downgradeModule<T>( | ||
This comment has been minimized.
Sorry, something went wrong. |
||
moduleFactoryOrBootstrapFn: NgModuleFactory<T>| | ||
((extraProviders: Provider[]) => Promise<NgModuleRef<T>>)): string { | ||
const LAZY_MODULE_NAME = UPGRADE_MODULE_NAME + '.lazy'; | ||
const bootstrapFn = isFunction(moduleFactoryOrBootstrapFn) ? | ||
moduleFactoryOrBootstrapFn : | ||
(extraProviders: Provider[]) => | ||
platformBrowser(extraProviders).bootstrapModuleFactory(moduleFactoryOrBootstrapFn); | ||
|
||
let injector: Injector; | ||
|
||
// Create an ng1 module to bootstrap. | ||
angular.module(LAZY_MODULE_NAME, []) | ||
.factory( | ||
INJECTOR_KEY, | ||
() => { | ||
if (!injector) { | ||
throw new Error('The Angular module has not been bootstrapped yet.'); | ||
} | ||
return injector; | ||
}) | ||
.factory(LAZY_MODULE_REF, [ | ||
$INJECTOR, | ||
($injector: angular.IInjectorService) => { | ||
const result: LazyModuleRef = { | ||
promise: bootstrapFn(angular1Providers).then(ref => { | ||
setTempInjectorRef($injector); | ||
|
||
injector = result.injector = new NgAdapterInjector(ref.injector); | ||
injector.get($INJECTOR); | ||
|
||
return injector; | ||
}) | ||
}; | ||
return result; | ||
} | ||
]); | ||
|
||
return LAZY_MODULE_NAME; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @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 | ||
*/ | ||
|
||
import {Injector, ɵNOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR as NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR} from '@angular/core'; | ||
|
||
|
||
export class NgAdapterInjector implements Injector { | ||
constructor(private modInjector: Injector) {} | ||
|
||
// When Angular locate a service in the component injector tree, the not found value is set to | ||
// `NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR`. In such a case we should not walk up to the module | ||
// injector. | ||
// AngularJS only supports a single tree and should always check the module injector. | ||
get(token: any, notFoundValue?: any): any { | ||
if (notFoundValue === NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR) { | ||
return notFoundValue; | ||
} | ||
|
||
return this.modInjector.get(token, notFoundValue); | ||
} | ||
} |
please add api docs