MCPcopy Index your code
hub / github.com/angular/angular / directiveFactory

Function directiveFactory

packages/upgrade/src/common/src/downgrade_component.ts:102–250  ·  view source on GitHub ↗
(
    $compile: ICompileService,
    $injector: IInjectorService,
    $parse: IParseService,
  )

Source from the content-addressed store, hash-verified

100 selectors?: string[];
101}): any /* angular.IInjectable */ {
102 const directiveFactory: IAnnotatedFunction = function (
103 $compile: ICompileService,
104 $injector: IInjectorService,
105 $parse: IParseService,
106 ): IDirective {
107 const unsafelyOverwriteSignalInputs =
108 (info as {unsafelyOverwriteSignalInputs?: boolean}).unsafelyOverwriteSignalInputs ?? false;
109 // When using `downgradeModule()`, we need to handle certain things specially. For example:
110 // - We always need to attach the component view to the `ApplicationRef` for it to be
111 // dirty-checked.
112 // - We need to ensure callbacks to Angular APIs (e.g. change detection) are run inside the
113 // Angular zone.
114 // NOTE: This is not needed, when using `UpgradeModule`, because `$digest()` will be run
115 // inside the Angular zone (except if explicitly escaped, in which case we shouldn't
116 // force it back in).
117 const isNgUpgradeLite = getUpgradeAppType($injector) === UpgradeAppType.Lite;
118 const wrapCallback: <T>(cb: () => T) => typeof cb = !isNgUpgradeLite
119 ? (cb) => cb
120 : (cb) => () => (NgZone.isInAngularZone() ? cb() : ngZone.run(cb));
121 let ngZone: NgZone;
122
123 // When downgrading multiple modules, special handling is needed wrt injectors.
124 const hasMultipleDowngradedModules = isNgUpgradeLite && getDowngradedModuleCount($injector) > 1;
125
126 return {
127 restrict: 'E',
128 terminal: true,
129 require: [REQUIRE_INJECTOR, REQUIRE_NG_MODEL],
130 // Controller needs to be set so that `angular-component-router.js` (from beta Angular 2)
131 // configuration properties can be made available. See:
132 // See G3: javascript/angular2/angular1_router_lib.js
133 // https://github.com/angular/angular.js/blob/47bf11ee94664367a26ed8c91b9b586d3dd420f5/src/ng/compile.js#L1670-L1691.
134 controller: function () {},
135 link: (scope: IScope, element: IAugmentedJQuery, attrs: IAttributes, required: any[]) => {
136 // We might have to compile the contents asynchronously, because this might have been
137 // triggered by `UpgradeNg1ComponentAdapterBuilder`, before the Angular templates have
138 // been compiled.
139
140 const ngModel: INgModelController = required[1];
141 const parentInjector: Injector | Thenable<Injector> | undefined = required[0];
142 let moduleInjector: Injector | Thenable<Injector> | undefined = undefined;
143 let ranAsync = false;
144
145 if (!parentInjector || hasMultipleDowngradedModules) {
146 const downgradedModule = info.downgradedModule || '';
147 const lazyModuleRefKey = `${LAZY_MODULE_REF}${downgradedModule}`;
148 const attemptedAction = `instantiating component '${getTypeName(info.component)}'`;
149
150 validateInjectionKey($injector, downgradedModule, lazyModuleRefKey, attemptedAction);
151
152 const lazyModuleRef = $injector.get(lazyModuleRefKey) as LazyModuleRef;
153 moduleInjector = lazyModuleRef.injector ?? lazyModuleRef.promise;
154 }
155
156 // Notes:
157 //
158 // There are two injectors: `finalModuleInjector` and `finalParentInjector` (they might be
159 // the same instance, but that is irrelevant):

Callers

nothing calls this directly

Calls 12

getUpgradeAppTypeFunction · 0.90
getDowngradedModuleCountFunction · 0.90
getTypeNameFunction · 0.90
validateInjectionKeyFunction · 0.90
wrapCallbackFunction · 0.85
doDowngradeFunction · 0.85
isInAngularZoneMethod · 0.80
runMethod · 0.65
getMethod · 0.65
thenMethod · 0.65
cbFunction · 0.50
allMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…