Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit cc7e822

Browse files
committed
add DI section
1 parent 543f6d9 commit cc7e822

11 files changed

+110
-115
lines changed

public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject-additional.component.es6

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ class TitleComponent {
2020
msg = '';
2121
constructor(
2222
@Inject('titlePrefix') @Optional() titlePrefix,
23-
@Attribute('title') title) {
24-
this.titlePrefix = titlePrefix;
25-
this.title = title;
23+
@Attribute('title') title
24+
) {
25+
this.titlePrefix = titlePrefix;
26+
this.title = title;
2627
}
2728

2829
ok() {

public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject.component.es6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { BrowserModule } from '@angular/platform-browser';
99
class HeroComponent {
1010
constructor(@Inject('heroName') name) {
1111
this.name = name;
12-
}
12+
}
1313
}
1414
// #enddocregion
1515

public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di.component.es6

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DataService } from './data.service';
88
selector: 'hero-di',
99
template: `<h1>Hero: {{name}}</h1>`
1010
})
11+
1112
class HeroComponent {
1213
name;
1314
constructor(dataService: DataService) {

public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
'<p>{{ msg }}</p>'
1010
}).Class({
1111
constructor: [
12-
[
13-
new ng.core.Optional(),
14-
new ng.core.Inject('titlePrefix')
15-
],
12+
[ new ng.core.Optional(), new ng.core.Inject('titlePrefix') ],
1613
new ng.core.Attribute('title'),
1714
function(titlePrefix, title) {
1815
this.titlePrefix = titlePrefix;
@@ -33,7 +30,7 @@
3330
}).Class({
3431
constructor: function() { }
3532
});
36-
33+
3734
app.HeroesDIInjectAdditionalModule =
3835
ng.core.NgModule({
3936
imports: [ ng.platformBrowser.BrowserModule ],

public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
.Class({
2626
constructor: function() {}
2727
});
28-
28+
2929
})(window.app = window.app || {});
3030

3131
(function(app) {
@@ -35,11 +35,12 @@
3535
template: '<h1>Hero: {{name}}</h1>'
3636
})
3737
.Class({
38-
constructor:
39-
[new ng.core.Inject('heroName'),
40-
function(name) {
41-
this.name = name;
42-
}]
38+
constructor: [
39+
new ng.core.Inject('heroName'),
40+
function(name) {
41+
this.name = name;
42+
}
43+
]
4344
});
4445
// #enddocregion ctor
4546

public/docs/_examples/cb-ts-to-js/js/app/hero-di-inline.component.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
template: '<h1>Hero: {{name}}</h1>'
66
})
77
.Class({
8-
constructor:
9-
[app.DataService,
10-
function(service) {
11-
this.name = service.getHeroName();
12-
}]
8+
constructor: [
9+
app.DataService,
10+
function(service) {
11+
this.name = service.getHeroName();
12+
}
13+
]
1314
});
1415
// #enddocregion
1516

public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
function HeroComponent(dataService) {
77
this.name = dataService.getHeroName();
88
}
9+
910
HeroComponent.parameters = [
1011
app.DataService
1112
];
13+
1214
HeroComponent.annotations = [
1315
new ng.core.Component({
1416
selector: 'hero-di',

public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ import { BrowserModule } from '@angular/platform-browser';
1919
class TitleComponent {
2020
private msg: string = '';
2121
constructor(
22-
@Inject('titlePrefix')
23-
@Optional()
24-
private titlePrefix: string,
25-
@Attribute('title')
26-
private title: string) {
27-
}
22+
@Inject('titlePrefix') @Optional() private titlePrefix: string,
23+
@Attribute('title') private title: string
24+
) { }
2825

2926
ok() {
3027
this.msg = 'OK!';

public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import { BrowserModule } from '@angular/platform-browser';
77
template: `<h1>Hero: {{name}}</h1>`
88
})
99
class HeroComponent {
10-
constructor(
11-
@Inject('heroName')
12-
private name: string) {
13-
}
10+
constructor(@Inject('heroName') private name: string) { }
1411
}
1512
// #enddocregion
1613

public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DataService } from './data.service';
88
selector: 'hero-di',
99
template: `<h1>Hero: {{name}}</h1>`
1010
})
11+
1112
class HeroComponent {
1213
name: string;
1314
constructor(dataService: DataService) {

public/docs/js/latest/cookbook/ts-to-js.jade

Lines changed: 82 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ a(id="class-metadata")
170170
Each item in the array corresponds to a decorator.
171171

172172
The pattern of creating a constructor and decorating it with metadata is so common that Angular
173-
provides an alternative convenience Class API for it for ES5 Javascript.
173+
provides an alternative ES5 convenience class API for it for ES5 Javascript.
174174
This API lets us define everything in a single expression.
175175

176176
With this API we first call the `ng.core.Component` function, followed by a chained `Class`
@@ -264,102 +264,99 @@ a(id="property-metadata")
264264
:marked
265265
## Dependency Injection
266266

267-
- var top="vertical-align:top"
268-
table(width="100%")
269-
col(width="50%")
270-
col(width="50%")
271-
tr
272-
th TypeScript
273-
th ES5 JavaScript
274-
tr(style=top)
275-
td
276-
:marked
277-
### Injection by Type
267+
### Injection by Type
278268

279-
Angular can often use TypeScript type information to
280-
determine what needs to be injected.
281-
282-
+makeExample('cb-ts-to-js/ts/app/hero-di.component.ts')(format="." )
283-
284-
td
285-
:marked
286-
### Injection with Parameter Tokens
287-
288-
Since no type information is available in ES5 JavaScript,
289-
we must identify "injectables" in some other way.
290-
291-
We attach a `parameters` array to the constructor function.
292-
Each array item is the dependency injection token that identifies the thing to be injected.
293-
Often the token is the constructor function for the class-like dependency.
294-
295-
+makeExample('cb-ts-to-js/js/app/hero-di.component.js')(format="." )
296-
297-
:marked
298-
When using the class convenience API, we can also supply the parameter
299-
tokens by wrapping the constructor in an array.
269+
Angular can often use TypeScript type information to determine what needs to be injected.
270+
ES6 with decorators can also make use of type information.
300271

301-
+makeExample('cb-ts-to-js/js/app/hero-di-inline.component.js')(format="." )
272+
Since no type information is available in ES5/6 JavaScript, we must identify "injectables" in
273+
some other way.
302274

303-
tr(style=top)
304-
td
305-
:marked
306-
### Injection with the @Inject decorator
307-
308-
When the thing being injected doesn't correspond directly to a type,
309-
we use the `@Inject()` decorator to supply the injection token.
310-
311-
In this example, we're injecting a string identified by the "heroName" token.
312-
313-
+makeExample('cb-ts-to-js/ts/app/hero-di-inject.component.ts')(format="." )
314-
315-
td
316-
:marked
317-
### Injection with plain string tokens
318-
319-
In JavaScript we add the token string to the injection parameters array.
275+
We attach a `parameters` array to the constructor function.
276+
Each array item is the dependency injection token that identifies the thing to be injected.
277+
Often the token is the constructor function for the class-like dependency.
278+
279+
In ES6 the decorators need to be inside a nested array.
280+
When using the ES5 class convenience API, we can also supply the parameter tokens by wrapping
281+
the constructor in an array.
320282

321-
+makeExample('cb-ts-to-js/js/app/hero-di-inject.component.js','parameters')(format="." )
322-
323-
:marked
324-
Alternatively, we can create a token with the `Inject` method and
325-
add that to the constructor array in the annotations like this:
326-
327-
+makeExample('cb-ts-to-js/js/app/hero-di-inject.component.js','ctor')(format="." )
283+
+makeTabs(`
284+
cb-ts-to-js/ts/app/hero-di.component.ts,
285+
cb-ts-to-js/js-es6-decorators/app/hero-di.component.es6,
286+
cb-ts-to-js/js-es6/app/hero-di.component.es6,
287+
cb-ts-to-js/js/app/hero-di.component.js,
288+
cb-ts-to-js/js/app/hero-di-inline.component.js
289+
`,`
290+
`,`
291+
Typescript,
292+
ES6 Javascript with decorators,
293+
ES6 Javascript,
294+
ES5 Javascript,
295+
ES5 Javascript with Class API
296+
`)
328297

329-
tr(style=top)
330-
td
331-
:marked
332-
### Additional Injection Decorators
298+
:marked
299+
### Injection with the @Inject decorator
333300

334-
We can attach additional decorators to constructor parameters
335-
to qualify the injection behavior. We can mark
336-
optional dependencies with the [`@Optional`](../api/core/index/Optional-decorator.html),
337-
inject host element attributes with [`@Attribute`](../api/core/index/Attribute-interface.html),
338-
inject content child queries with [`@ContentChild`](../api/core/index/ContentChild-decorator.html)
339-
and inject view child queries with [`@ViewChild`](../api/core/index/ViewChild-decorator.html)).
301+
When the thing being injected doesn't correspond directly to a type, we use the
302+
`@Inject()` decorator to supply the injection token.
303+
In this example, we're injecting a string identified by the "heroName" token.
340304

341-
+makeExample('cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts')(format="." )
305+
In ES5/6 JavaScript we add the token string to the injection parameters array.
306+
Alternatively, when using the ES5 convenience class API we can create a token with the
307+
`Inject` method and add that to the constructor array in the annotations.
342308

343-
td
344-
:marked
345-
### Additional Injection Metadata with Nested Arrays
309+
+makeTabs(`
310+
cb-ts-to-js/ts/app/hero-di-inject.component.ts,
311+
cb-ts-to-js/js-es6-decorators/app/hero-di-inject.component.es6,
312+
cb-ts-to-js/js-es6/app/hero-di-inject.component.es6,
313+
cb-ts-to-js/js/app/hero-di-inject.component.js,
314+
cb-ts-to-js/js/app/hero-di-inject.component.js
315+
`,`
316+
,
317+
,
318+
,
319+
parameters,
320+
ctor
321+
`,`
322+
Typescript,
323+
ES6 Javascript with decorators,
324+
ES6 Javascript,
325+
ES5 Javascript,
326+
ES5 Javascript with Class API
327+
`)
346328

347-
To achieve the same effect in JavaScript, use the constructor array notation
348-
in which the injection information precedes the constructor function itself.
349-
350-
Use the injection support functions `Attribute`, `Host`, `Optional`, `Self`, `SkipSelf` to qualify dependency injection behavior.
351-
352-
Use a nested array to combine injection functions.
329+
:marked
330+
### Additional Injection Decorators
353331

354-
+makeExample('cb-ts-to-js/js/app/hero-di-inject-additional.component.js')(format="." )
332+
We can attach additional decorators to constructor parameters to qualify the injection behavior.
333+
We can mark optional dependencies with the [`@Optional`](../api/core/index/Optional-decorator.html),
334+
inject host element attributes with [`@Attribute`](../api/core/index/Attribute-interface.html),
335+
inject content child queries with [`@ContentChild`](../api/core/index/ContentChild-decorator.html)
336+
and inject view child queries with [`@ViewChild`](../api/core/index/ViewChild-decorator.html)).
355337

356-
:marked
357-
We can apply other additional parameter decorators such as
358-
[`@Host`](../api/core/index/Host-decorator.html) and
359-
[`@SkipSelf`](../api/core/index/SkipSelf-decorator.html) in the same way -
360-
by adding `new ng.core.Host()` or `ng.core.SkipSelf()` in the
361-
parameters array.
338+
In ES6 Javascript we just add the extra decorators to the nested injection parameters array.
362339

340+
To achieve the same effect in ES5 JavaScript, use the a nested array with the constructor
341+
array notation in which the injection information precedes the constructor function itself.
342+
343+
We can apply other additional parameter decorators such as
344+
[`@Host`](../api/core/index/Host-decorator.html) and
345+
[`@SkipSelf`](../api/core/index/SkipSelf-decorator.html) in the same way -
346+
by adding `new ng.core.Host()` or `ng.core.SkipSelf()` in the
347+
parameters array.
348+
+makeTabs(`
349+
cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts,
350+
cb-ts-to-js/js-es6-decorators/app/hero-di-inject-additional.component.es6,
351+
cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6,
352+
cb-ts-to-js/js/app/hero-di-inject-additional.component.js
353+
`,`
354+
`,`
355+
Typescript,
356+
ES6 Javascript with decorators,
357+
ES6 Javascript,
358+
ES5 Javascript
359+
`)
363360

364361
a(id="other-property-metadata")
365362
.l-main-section

0 commit comments

Comments
 (0)