UNPKG

16.5 kBTypeScriptView Raw
1/**
2 * @license Angular v19.0.5
3 * (c) 2010-2024 Google LLC. https://angular.io/
4 * License: MIT
5 */
6
7
8import { CompilerOptions } from '@angular/core';
9import { Injector } from '@angular/core';
10import { NgModuleRef } from '@angular/core';
11import { Type } from '@angular/core';
12import { Version } from '@angular/core';
13
14declare interface IAngularBootstrapConfig {
15 strictDi?: boolean;
16}
17
18declare interface IInjectorService {
19 get(key: string): any;
20 has(key: string): boolean;
21}
22
23declare interface IRootScopeService {
24 $new(isolate?: boolean): IScope;
25 $id: string;
26 $parent: IScope;
27 $root: IScope;
28 $watch(exp: Ng1Expression, fn?: (a1?: any, a2?: any) => void): Function;
29 $on(event: string, fn?: (event?: any, ...args: any[]) => void): Function;
30 $destroy(): any;
31 $apply(exp?: Ng1Expression): any;
32 $digest(): any;
33 $evalAsync(exp: Ng1Expression, locals?: any): void;
34 $on(event: string, fn?: (event?: any, ...args: any[]) => void): Function;
35 $$childTail: IScope;
36 $$childHead: IScope;
37 $$nextSibling: IScope;
38 $$phase: any;
39 [key: string]: any;
40}
41
42declare interface IScope extends IRootScopeService {
43}
44
45declare type Ng1Expression = string | Function;
46
47/**
48 * Use `UpgradeAdapter` to allow AngularJS and Angular to coexist in a single application.
49 *
50 * The `UpgradeAdapter` allows:
51 * 1. creation of Angular component from AngularJS component directive
52 * (See {@link UpgradeAdapter#upgradeNg1Component})
53 * 2. creation of AngularJS directive from Angular component.
54 * (See {@link UpgradeAdapter#downgradeNg2Component})
55 * 3. Bootstrapping of a hybrid Angular application which contains both of the frameworks
56 * coexisting in a single application.
57 *
58 * @usageNotes
59 * ### Mental Model
60 *
61 * When reasoning about how a hybrid application works it is useful to have a mental model which
62 * describes what is happening and explains what is happening at the lowest level.
63 *
64 * 1. There are two independent frameworks running in a single application, each framework treats
65 * the other as a black box.
66 * 2. Each DOM element on the page is owned exactly by one framework. Whichever framework
67 * instantiated the element is the owner. Each framework only updates/interacts with its own
68 * DOM elements and ignores others.
69 * 3. AngularJS directives always execute inside AngularJS framework codebase regardless of
70 * where they are instantiated.
71 * 4. Angular components always execute inside Angular framework codebase regardless of
72 * where they are instantiated.
73 * 5. An AngularJS component can be upgraded to an Angular component. This creates an
74 * Angular directive, which bootstraps the AngularJS component directive in that location.
75 * 6. An Angular component can be downgraded to an AngularJS component directive. This creates
76 * an AngularJS directive, which bootstraps the Angular component in that location.
77 * 7. Whenever an adapter component is instantiated the host element is owned by the framework
78 * doing the instantiation. The other framework then instantiates and owns the view for that
79 * component. This implies that component bindings will always follow the semantics of the
80 * instantiation framework. The syntax is always that of Angular syntax.
81 * 8. AngularJS is always bootstrapped first and owns the bottom most view.
82 * 9. The new application is running in Angular zone, and therefore it no longer needs calls to
83 * `$apply()`.
84 *
85 * ### Example
86 *
87 * ```ts
88 * const adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module), myCompilerOptions);
89 * const module = angular.module('myExample', []);
90 * module.directive('ng2Comp', adapter.downgradeNg2Component(Ng2Component));
91 *
92 * module.directive('ng1Hello', function() {
93 * return {
94 * scope: { title: '=' },
95 * template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'
96 * };
97 * });
98 *
99 *
100 * @Component({
101 * selector: 'ng2-comp',
102 * inputs: ['name'],
103 * template: 'ng2[<ng1-hello [title]="name">transclude</ng1-hello>](<ng-content></ng-content>)',
104 * directives:
105 * })
106 * class Ng2Component {
107 * }
108 *
109 * @NgModule({
110 * declarations: [Ng2Component, adapter.upgradeNg1Component('ng1Hello')],
111 * imports: [BrowserModule]
112 * })
113 * class MyNg2Module {}
114 *
115 *
116 * document.body.innerHTML = '<ng2-comp name="World">project</ng2-comp>';
117 *
118 * adapter.bootstrap(document.body, ['myExample']).ready(function() {
119 * expect(document.body.textContent).toEqual(
120 * "ng2[ng1[Hello World!](transclude)](project)");
121 * });
122 *
123 * ```
124 *
125 * @deprecated Deprecated since v5. Use `upgrade/static` instead, which also supports
126 * [Ahead-of-Time compilation](tools/cli/aot-compiler).
127 * @publicApi
128 */
129export declare class UpgradeAdapter {
130 private ng2AppModule;
131 private compilerOptions?;
132 private idPrefix;
133 private downgradedComponents;
134 private upgradedProviders;
135 private moduleRef;
136 constructor(ng2AppModule: Type<any>, compilerOptions?: CompilerOptions | undefined);
137 /**
138 * Allows Angular Component to be used from AngularJS.
139 *
140 * Use `downgradeNg2Component` to create an AngularJS Directive Definition Factory from
141 * Angular Component. The adapter will bootstrap Angular component from within the
142 * AngularJS template.
143 *
144 * @usageNotes
145 * ### Mental Model
146 *
147 * 1. The component is instantiated by being listed in AngularJS template. This means that the
148 * host element is controlled by AngularJS, but the component's view will be controlled by
149 * Angular.
150 * 2. Even thought the component is instantiated in AngularJS, it will be using Angular
151 * syntax. This has to be done, this way because we must follow Angular components do not
152 * declare how the attributes should be interpreted.
153 * 3. `ng-model` is controlled by AngularJS and communicates with the downgraded Angular component
154 * by way of the `ControlValueAccessor` interface from @angular/forms. Only components that
155 * implement this interface are eligible.
156 *
157 * ### Supported Features
158 *
159 * - Bindings:
160 * - Attribute: `<comp name="World">`
161 * - Interpolation: `<comp greeting="Hello {{name}}!">`
162 * - Expression: `<comp [name]="username">`
163 * - Event: `<comp (close)="doSomething()">`
164 * - ng-model: `<comp ng-model="name">`
165 * - Content projection: yes
166 *
167 * ### Example
168 *
169 * ```
170 * const adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module));
171 * const module = angular.module('myExample', []);
172 * module.directive('greet', adapter.downgradeNg2Component(Greeter));
173 *
174 * @Component({
175 * selector: 'greet',
176 * template: '{{salutation}} {{name}}! - <ng-content></ng-content>'
177 * })
178 * class Greeter {
179 * @Input() salutation: string;
180 * @Input() name: string;
181 * }
182 *
183 * @NgModule({
184 * declarations: [Greeter],
185 * imports: [BrowserModule]
186 * })
187 * class MyNg2Module {}
188 *
189 * document.body.innerHTML =
190 * 'ng1 template: <greet salutation="Hello" [name]="world">text</greet>';
191 *
192 * adapter.bootstrap(document.body, ['myExample']).ready(function() {
193 * expect(document.body.textContent).toEqual("ng1 template: Hello world! - text");
194 * });
195 * ```
196 */
197 downgradeNg2Component(component: Type<any>): Function;
198 /**
199 * Allows AngularJS Component to be used from Angular.
200 *
201 * Use `upgradeNg1Component` to create an Angular component from AngularJS Component
202 * directive. The adapter will bootstrap AngularJS component from within the Angular
203 * template.
204 *
205 * @usageNotes
206 * ### Mental Model
207 *
208 * 1. The component is instantiated by being listed in Angular template. This means that the
209 * host element is controlled by Angular, but the component's view will be controlled by
210 * AngularJS.
211 *
212 * ### Supported Features
213 *
214 * - Bindings:
215 * - Attribute: `<comp name="World">`
216 * - Interpolation: `<comp greeting="Hello {{name}}!">`
217 * - Expression: `<comp [name]="username">`
218 * - Event: `<comp (close)="doSomething()">`
219 * - Transclusion: yes
220 * - Only some of the features of
221 * [Directive Definition Object](https://docs.angularjs.org/api/ng/service/$compile) are
222 * supported:
223 * - `compile`: not supported because the host element is owned by Angular, which does
224 * not allow modifying DOM structure during compilation.
225 * - `controller`: supported. (NOTE: injection of `$attrs` and `$transclude` is not supported.)
226 * - `controllerAs`: supported.
227 * - `bindToController`: supported.
228 * - `link`: supported. (NOTE: only pre-link function is supported.)
229 * - `name`: supported.
230 * - `priority`: ignored.
231 * - `replace`: not supported.
232 * - `require`: supported.
233 * - `restrict`: must be set to 'E'.
234 * - `scope`: supported.
235 * - `template`: supported.
236 * - `templateUrl`: supported.
237 * - `terminal`: ignored.
238 * - `transclude`: supported.
239 *
240 *
241 * ### Example
242 *
243 * ```
244 * const adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module));
245 * const module = angular.module('myExample', []);
246 *
247 * module.directive('greet', function() {
248 * return {
249 * scope: {salutation: '=', name: '=' },
250 * template: '{{salutation}} {{name}}! - <span ng-transclude></span>'
251 * };
252 * });
253 *
254 * module.directive('ng2', adapter.downgradeNg2Component(Ng2Component));
255 *
256 * @Component({
257 * selector: 'ng2',
258 * template: 'ng2 template: <greet salutation="Hello" [name]="world">text</greet>'
259 * })
260 * class Ng2Component {
261 * }
262 *
263 * @NgModule({
264 * declarations: [Ng2Component, adapter.upgradeNg1Component('greet')],
265 * imports: [BrowserModule]
266 * })
267 * class MyNg2Module {}
268 *
269 * document.body.innerHTML = '<ng2></ng2>';
270 *
271 * adapter.bootstrap(document.body, ['myExample']).ready(function() {
272 * expect(document.body.textContent).toEqual("ng2 template: Hello world! - text");
273 * });
274 * ```
275 */
276 upgradeNg1Component(name: string): Type<any>;
277 /**
278 * Registers the adapter's AngularJS upgrade module for unit testing in AngularJS.
279 * Use this instead of `angular.mock.module()` to load the upgrade module into
280 * the AngularJS testing injector.
281 *
282 * @usageNotes
283 * ### Example
284 *
285 * ```
286 * const upgradeAdapter = new UpgradeAdapter(MyNg2Module);
287 *
288 * // configure the adapter with upgrade/downgrade components and services
289 * upgradeAdapter.downgradeNg2Component(MyComponent);
290 *
291 * let upgradeAdapterRef: UpgradeAdapterRef;
292 * let $compile, $rootScope;
293 *
294 * // We must register the adapter before any calls to `inject()`
295 * beforeEach(() => {
296 * upgradeAdapterRef = upgradeAdapter.registerForNg1Tests(['heroApp']);
297 * });
298 *
299 * beforeEach(inject((_$compile_, _$rootScope_) => {
300 * $compile = _$compile_;
301 * $rootScope = _$rootScope_;
302 * }));
303 *
304 * it("says hello", (done) => {
305 * upgradeAdapterRef.ready(() => {
306 * const element = $compile("<my-component></my-component>")($rootScope);
307 * $rootScope.$apply();
308 * expect(element.html()).toContain("Hello World");
309 * done();
310 * })
311 * });
312 *
313 * ```
314 *
315 * @param modules any AngularJS modules that the upgrade module should depend upon
316 * @returns an `UpgradeAdapterRef`, which lets you register a `ready()` callback to
317 * run assertions once the Angular components are ready to test through AngularJS.
318 */
319 registerForNg1Tests(modules?: string[]): UpgradeAdapterRef;
320 /**
321 * Bootstrap a hybrid AngularJS / Angular application.
322 *
323 * This `bootstrap` method is a direct replacement (takes same arguments) for AngularJS
324 * [`bootstrap`](https://docs.angularjs.org/api/ng/function/angular.bootstrap) method. Unlike
325 * AngularJS, this bootstrap is asynchronous.
326 *
327 * @usageNotes
328 * ### Example
329 *
330 * ```
331 * const adapter = new UpgradeAdapter(MyNg2Module);
332 * const module = angular.module('myExample', []);
333 * module.directive('ng2', adapter.downgradeNg2Component(Ng2));
334 *
335 * module.directive('ng1', function() {
336 * return {
337 * scope: { title: '=' },
338 * template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'
339 * };
340 * });
341 *
342 *
343 * @Component({
344 * selector: 'ng2',
345 * inputs: ['name'],
346 * template: 'ng2[<ng1 [title]="name">transclude</ng1>](<ng-content></ng-content>)'
347 * })
348 * class Ng2 {
349 * }
350 *
351 * @NgModule({
352 * declarations: [Ng2, adapter.upgradeNg1Component('ng1')],
353 * imports: [BrowserModule]
354 * })
355 * class MyNg2Module {}
356 *
357 * document.body.innerHTML = '<ng2 name="World">project</ng2>';
358 *
359 * adapter.bootstrap(document.body, ['myExample']).ready(function() {
360 * expect(document.body.textContent).toEqual(
361 * "ng2[ng1[Hello World!](transclude)](project)");
362 * });
363 * ```
364 */
365 bootstrap(element: Element, modules?: any[], config?: IAngularBootstrapConfig): UpgradeAdapterRef;
366 /**
367 * Allows AngularJS service to be accessible from Angular.
368 *
369 * @usageNotes
370 * ### Example
371 *
372 * ```
373 * class Login { ... }
374 * class Server { ... }
375 *
376 * @Injectable()
377 * class Example {
378 * constructor(@Inject('server') server, login: Login) {
379 * ...
380 * }
381 * }
382 *
383 * const module = angular.module('myExample', []);
384 * module.service('server', Server);
385 * module.service('login', Login);
386 *
387 * const adapter = new UpgradeAdapter(MyNg2Module);
388 * adapter.upgradeNg1Provider('server');
389 * adapter.upgradeNg1Provider('login', {asToken: Login});
390 *
391 * adapter.bootstrap(document.body, ['myExample']).ready((ref) => {
392 * const example: Example = ref.ng2Injector.get(Example);
393 * });
394 *
395 * ```
396 */
397 upgradeNg1Provider(name: string, options?: {
398 asToken: any;
399 }): void;
400 /**
401 * Allows Angular service to be accessible from AngularJS.
402 *
403 * @usageNotes
404 * ### Example
405 *
406 * ```
407 * class Example {
408 * }
409 *
410 * const adapter = new UpgradeAdapter(MyNg2Module);
411 *
412 * const module = angular.module('myExample', []);
413 * module.factory('example', adapter.downgradeNg2Provider(Example));
414 *
415 * adapter.bootstrap(document.body, ['myExample']).ready((ref) => {
416 * const example: Example = ref.ng1Injector.get('example');
417 * });
418 *
419 * ```
420 */
421 downgradeNg2Provider(token: any): Function;
422 /**
423 * Declare the AngularJS upgrade module for this adapter without bootstrapping the whole
424 * hybrid application.
425 *
426 * This method is automatically called by `bootstrap()` and `registerForNg1Tests()`.
427 *
428 * @param modules The AngularJS modules that this upgrade module should depend upon.
429 * @returns The AngularJS upgrade module that is declared by this method
430 *
431 * @usageNotes
432 * ### Example
433 *
434 * ```
435 * const upgradeAdapter = new UpgradeAdapter(MyNg2Module);
436 * upgradeAdapter.declareNg1Module(['heroApp']);
437 * ```
438 */
439 private declareNg1Module;
440}
441
442/**
443 * Use `UpgradeAdapterRef` to control a hybrid AngularJS / Angular application.
444 *
445 * @deprecated Deprecated since v5. Use `upgrade/static` instead, which also supports
446 * [Ahead-of-Time compilation](tools/cli/aot-compiler).
447 * @publicApi
448 */
449export declare class UpgradeAdapterRef {
450 ng1RootScope: IRootScopeService;
451 ng1Injector: IInjectorService;
452 ng2ModuleRef: NgModuleRef<any>;
453 ng2Injector: Injector;
454 /**
455 * Register a callback function which is notified upon successful hybrid AngularJS / Angular
456 * application has been bootstrapped.
457 *
458 * The `ready` callback function is invoked inside the Angular zone, therefore it does not
459 * require a call to `$apply()`.
460 */
461 ready(fn: (upgradeAdapterRef: UpgradeAdapterRef) => void): void;
462 /**
463 * Dispose of running hybrid AngularJS / Angular application.
464 */
465 dispose(): void;
466}
467
468/**
469 * @publicApi
470 */
471export declare const VERSION: Version;
472
473export { }