Skip to content

Commit 486c1ed

Browse files
committed
docs(ngUpgrade): document public methods
1 parent 053b7a5 commit 486c1ed

File tree

1 file changed

+162
-1
lines changed

1 file changed

+162
-1
lines changed

modules/upgrade/src/upgrade_adapter.ts

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var upgradeCount: number = 0;
7979
* ```
8080
* var adapter = new UpgradeAdapter();
8181
* var module = angular.module('myExample', []);
82+
* module.directive('ng2', adapter.downgradeNg2Component(Ng2));
8283
*
8384
* module.directive('ng1', function() {
8485
* return {
@@ -97,7 +98,7 @@ var upgradeCount: number = 0;
9798
* class Ng2 {
9899
* }
99100
*
100-
* document.body = '<ng2 name="World">project</ng2>';
101+
* document.body.innerHTML = '<ng2 name="World">project</ng2>';
101102
*
102103
* adapter.bootstrap(document.body, ['myExample']).ready(function() {
103104
* expect(document.body.textContent).toEqual(
@@ -113,12 +114,133 @@ export class UpgradeAdapter {
113114
/* @internal */
114115
private downgradedComponents: {[name: string]: UpgradeNg1ComponentAdapterBuilder} = {};
115116

117+
/**
118+
* Allows Angular v2 Component to be used from AngularJS v1.
119+
*
120+
* Use `downgradeNg2Component` to create an AngularJS v1 Directive Definition Factory from
121+
* Angular v2 Component. The adapter will bootstrap Angular v2 component from within the
122+
* AngularJS v1 template.
123+
*
124+
* ## Mental Model
125+
*
126+
* 1. The component is instantiated by being listed in AngularJS v1 template. This means that the
127+
* host element is controlled by AngularJS v1, but the component's view will be controlled by
128+
* Angular v2.
129+
* 2. Even thought the component is instantiated in AngularJS v1, it will be using Angular v2
130+
* syntax. This has to be done, this way because we must follow Angular v2 components do not
131+
* declare how the attributes should be interpreted.
132+
*
133+
* ## Supported Features
134+
*
135+
* - Bindings:
136+
* - Attribute: `<comp name="World">`
137+
* - Interpolation: `<comp greeting="Hello {{name}}!">`
138+
* - Expression: `<comp [name]="username">`
139+
* - Event: `<comp (close)="doSomething()">`
140+
* - Content projection: yes
141+
*
142+
* ## Example
143+
*
144+
* ```
145+
* var adapter = new UpgradeAdapter();
146+
* var module = angular.module('myExample', []);
147+
* module.directive('greet', adapter.downgradeNg2Component(Greeter));
148+
*
149+
* @Component({
150+
* selector: 'greet',
151+
* template: '{{salutation}} {{name}}! - <ng-content></ng-content>'
152+
* })
153+
* class Greeter {
154+
* @Input() salutation: string;
155+
* @Input() name: string;
156+
* }
157+
*
158+
* document.body.innerHTML =
159+
* 'ng1 template: <greet salutation="Hello" [name]="world">text</greet>';
160+
*
161+
* adapter.bootstrap(document.body, ['myExample']).ready(function() {
162+
* expect(document.body.textContent).toEqual("ng1 template: Hello world! - text");
163+
* });
164+
* ```
165+
*/
116166
downgradeNg2Component(type: Type): Function {
117167
this.upgradedComponents.push(type);
118168
var info: ComponentInfo = getComponentInfo(type);
119169
return ng1ComponentDirective(info, `${this.idPrefix}${info.selector}_c`);
120170
}
121171

172+
/**
173+
* Allows AngularJS v1 Component to be used from Angular v2.
174+
*
175+
* Use `upgradeNg1Component` to create an Angular v2 component from AngularJS v1 Component
176+
* directive. The adapter will bootstrap AngularJS v1 component from within the Angular v2
177+
* template.
178+
*
179+
* ## Mental Model
180+
*
181+
* 1. The component is instantiated by being listed in Angular v2 template. This means that the
182+
* host element is controlled by Angular v2, but the component's view will be controlled by
183+
* AngularJS v1.
184+
*
185+
* ## Supported Features
186+
*
187+
* - Bindings:
188+
* - Attribute: `<comp name="World">`
189+
* - Interpolation: `<comp greeting="Hello {{name}}!">`
190+
* - Expression: `<comp [name]="username">`
191+
* - Event: `<comp (close)="doSomething()">`
192+
* - Transclusion: yes
193+
* - Only some of the features of
194+
* [Directive Definition Object](https://docs.angularjs.org/api/ng/service/$compile) are
195+
* supported:
196+
* - `compile`: not supported because the host element is owned by Angular v2, which does
197+
* not allow modifying DOM structure during compilation.
198+
* - `controller`: supported. (NOTE: injection of `$attrs` and `$transclude` is not supported.)
199+
* - `controllerAs': supported.
200+
* - `bindToController': supported.
201+
* - `link': supported. (NOTE: only pre-link function is supported.)
202+
* - `name': supported.
203+
* - `priority': ignored.
204+
* - `replace': not supported.
205+
* - `require`: supported.
206+
* - `restrict`: must be set to 'E'.
207+
* - `scope`: supported.
208+
* - `template`: supported.
209+
* - `templateUrl`: supported.
210+
* - `terminal`: ignored.
211+
* - `transclude`: supported.
212+
*
213+
*
214+
* ## Example
215+
*
216+
* ```
217+
* var adapter = new UpgradeAdapter();
218+
* var module = angular.module('myExample', []);
219+
*
220+
* module.directive('greet', function() {
221+
* return {
222+
* scope: {salutation: '=', name: '=' },
223+
* template: '{{salutation}} {{name}}! - <span ng-transclude></span>'
224+
* };
225+
* });
226+
*
227+
* module.directive('ng2', adapter.downgradeNg2Component(Ng2));
228+
*
229+
* @Component({
230+
* selector: 'ng2',
231+
* template: 'ng2 template: <greet salutation="Hello" [name]="world">text</greet>'
232+
* directives: [adapter.upgradeNg1Component('greet')]
233+
* })
234+
* class Ng2 {
235+
* }
236+
*
237+
* document.body.innerHTML = '<ng2></ng2>';
238+
*
239+
* adapter.bootstrap(document.body, ['myExample']).ready(function() {
240+
* expect(document.body.textContent).toEqual("ng2 template: Hello world! - text");
241+
* });
242+
* ```
243+
*/
122244
upgradeNg1Component(name: string): Type {
123245
if ((<any>this.downgradedComponents).hasOwnProperty(name)) {
124246
return this.downgradedComponents[name].type;
@@ -127,6 +249,45 @@ export class UpgradeAdapter {
127249
}
128250
}
129251

252+
/**
253+
* Bootstrap a hybrid AngularJS v1 / Angular v2 application.
254+
*
255+
* This `bootstrap` method is a direct replacement (takes same arguments) for AngularJS v1
256+
* [`bootstrap`](https://docs.angularjs.org/api/ng/function/angular.bootstrap) method. Unlike
257+
* AngularJS v1, this bootstrap is asynchronous.
258+
*
259+
* ## Example
260+
*
261+
* ```
262+
* var adapter = new UpgradeAdapter();
263+
* var module = angular.module('myExample', []);
264+
* module.directive('ng2', adapter.downgradeNg2Component(Ng2));
265+
*
266+
* module.directive('ng1', function() {
267+
* return {
268+
* scope: { title: '=' },
269+
* template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'
270+
* };
271+
* });
272+
*
273+
*
274+
* @Component({
275+
* selector: 'ng2',
276+
* inputs: ['name'],
277+
* template: 'ng2[<ng1 [title]="name">transclude</ng1>](<ng-content></ng-content>)',
278+
* directives: [adapter.upgradeNg1Component('ng1')]
279+
* })
280+
* class Ng2 {
281+
* }
282+
*
283+
* document.body.innerHTML = '<ng2 name="World">project</ng2>';
284+
*
285+
* adapter.bootstrap(document.body, ['myExample']).ready(function() {
286+
* expect(document.body.textContent).toEqual(
287+
* "ng2[ng1[Hello World!](transclude)](project)");
288+
* });
289+
* ```
290+
*/
130291
bootstrap(element: Element, modules?: any[],
131292
config?: angular.IAngularBootstrapConfig): UpgradeAdapterRef {
132293
var upgrade = new UpgradeAdapterRef();

0 commit comments

Comments
 (0)