UNPKG

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