Skip to content

Commit 89a0f24

Browse files
committed
doc(di): minor fixes
Closes angular#3614
1 parent 26d2ea8 commit 89a0f24

File tree

2 files changed

+64
-67
lines changed

2 files changed

+64
-67
lines changed

modules/angular2/docs/di/di.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ class Engine {
3232
}
3333
3434
class Car {
35-
constructor(@Inject(Engine) engine) {
36-
}
35+
constructor(@Inject(Engine) engine) {
36+
}
3737
}
3838
3939
var inj = Injector.resolveAndCreate([
40-
bind(Car).toClass(Car),
41-
bind(Engine).toClass(Engine)
40+
bind(Car).toClass(Car),
41+
bind(Engine).toClass(Engine)
4242
]);
4343
var car = inj.get(Car);
4444
```
4545

46-
In this example we create two bindings: one for Car and one for Engine. `@Inject(Engine)` declares a dependency on Engine.
46+
In this example we create two bindings: one for `Car` and one for `Engine`. `@Inject(Engine)` declares a dependency on Engine.
4747

4848
## Injector
4949

@@ -79,7 +79,7 @@ Injectors are hierarchical.
7979

8080
```
8181
var parent = Injector.resolveAndCreate([
82-
bind(Engine).toClass(TurboEngine)
82+
bind(Engine).toClass(TurboEngine)
8383
]);
8484
var child = parent.resolveAndCreateChild([Car]);
8585
@@ -89,8 +89,8 @@ var car = child.get(Car); // uses the Car binding from the child injector and En
8989
Injectors form a tree.
9090

9191
```
92-
GrandParentInjector
93-
/ \
92+
GrandParentInjector
93+
/ \
9494
Parent1Injector Parent2Injector
9595
|
9696
ChildInjector
@@ -103,10 +103,10 @@ The dependency resolution algorithm works as follows:
103103
var inj = this;
104104
while (inj) {
105105
if (inj.hasKey(requestedKey)) {
106-
return inj.get(requestedKey);
107-
} else {
108-
inj = inj.parent;
109-
}
106+
return inj.get(requestedKey);
107+
} else {
108+
inj = inj.parent;
109+
}
110110
}
111111
throw new NoBindingError(requestedKey);
112112
```
@@ -156,7 +156,7 @@ Dependency resolution only walks up the tree. The following will throw because D
156156

157157
```
158158
var parent = Injector.resolveAndCreate([Car]);
159-
var child = injector.resolveAndCreateChild([
159+
var child = parent.resolveAndCreateChild([
160160
bind(Engine).toClass(TurboEngine)
161161
]);
162162
@@ -170,40 +170,40 @@ You can bind to a class, a value, or a factory. It is also possible to alias exi
170170

171171
```
172172
var inj = Injector.resolveAndCreate([
173-
bind(Car).toClass(Car),
174-
bind(Engine).toClass(Engine)
173+
bind(Car).toClass(Car),
174+
bind(Engine).toClass(Engine)
175175
]);
176176
177177
var inj = Injector.resolveAndCreate([
178-
Car, // syntax sugar for bind(Car).toClass(Car)
179-
Engine
178+
Car, // syntax sugar for bind(Car).toClass(Car)
179+
Engine
180180
]);
181181
182182
var inj = Injector.resolveAndCreate([
183-
bind(Car).toValue(new Car(new Engine()))
183+
bind(Car).toValue(new Car(new Engine()))
184184
]);
185185
186186
var inj = Injector.resolveAndCreate([
187-
bind(Car).toFactory((e) => new Car(e), [Engine]),
188-
bind(Engine).toFactory(() => new Engine())
187+
bind(Car).toFactory((e) => new Car(e), [Engine]),
188+
bind(Engine).toFactory(() => new Engine())
189189
]);
190190
```
191191

192192
You can bind any token.
193193

194194
```
195195
var inj = Injector.resolveAndCreate([
196-
bind(Car).toFactory((e) => new Car(), ["engine!"]),
197-
bind("engine!").toClass(Engine)
196+
bind(Car).toFactory((e) => new Car(), ["engine!"]),
197+
bind("engine!").toClass(Engine)
198198
]);
199199
```
200200

201201
If you want to alias an existing binding, you can do so using `toAlias`:
202202

203203
```
204204
var inj = Injector.resolveAndCreate([
205-
bind(Engine).toClass(Engine),
206-
bind("engine!").toAlias(Engine)
205+
bind(Engine).toClass(Engine),
206+
bind("engine!").toAlias(Engine)
207207
]);
208208
```
209209
which implies `inj.get(Engine) === inj.get("engine!")`.

modules/angular2/docs/di/di_advanced.md

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Often there is a need to create multiple instances of essentially the same injec
3333
Doing the following would be very inefficient.
3434

3535
```
36-
function createComponetInjector(parent, bindings:Binding[]) {
37-
return parentresolveAndCreateChild(bindings);
36+
function createComponetInjector(parent, bindings: Binding[]) {
37+
return parent.resolveAndCreateChild(bindings);
3838
}
3939
```
4040

@@ -70,80 +70,77 @@ Imagine the following scenario:
7070
```
7171
ParentInjector
7272
/ \
73-
/ \ host
73+
/ \ host
7474
Child1 Child2
7575
```
7676

77-
Here both Child1 and Child2 are children of ParentInjector. Child2 marks this relationship as host. ParentInjector might want to expose two different sets of bindings for its "regular" children and its "host" children. Bindings visible to "regular" children are called PUBLIC, and bindings visible to "host" children are called PRIVATE. This is an advanced use case used by Angular, where components can provide different sets of bindings for their children and their view.
77+
Here both Child1 and Child2 are children of ParentInjector. Child2 marks this relationship as host. ParentInjector might want to expose two different sets of bindings for its "regular" children and its "host" children. Bindings visible to "regular" children are called "public" and bindings visible to "host" children are called "private". This is an advanced use case used by Angular, where components can provide different sets of bindings for their children and their view.
7878

7979
Let's look at this example.
8080

8181
```
8282
class Car {
8383
constructor(@Host() e: Engine) {}
8484
}
85-
var resolvedBindings = Injector.resolve([Car, Engine]);
8685
8786
var parentProto = new ProtoInjector([
88-
new BindingWithVisibility(Engine, PUBLIC),
89-
new BindingWithVisibility(Car, PUBLIC)
87+
new BindingWithVisibility(Engine, Visibility.Public),
88+
new BindingWithVisibility(Car, Visibility.Public)
9089
]);
9190
var parent = new Injector(parentProto);
9291
93-
var hostChildProto = new ProtoInjector([new BindingWithVisibility(Car, PUBLIC)]);
92+
var hostChildProto = new ProtoInjector([new BindingWithVisibility(Car, Visibility.Public)]);
9493
var hostChild = new Injector(hostChildProto, parent, true);
9594
96-
var regularChildProto = new ProtoInjector([new BindingWithVisibility(Car, PUBLIC)]);
95+
var regularChildProto = new ProtoInjector([new BindingWithVisibility(Car, Visibility.Public)]);
9796
var regularChild = new Injector(regularChildProto, parent, false);
9897
99-
hostChild.get(Car); // will throw because PUBLIC dependencies declared at the host cannot be seen by child injectors
98+
hostChild.get(Car); // will throw because public dependencies declared at the host cannot be seen by child injectors
10099
parent.get(Car); // this works
101100
regularChild.get(Car); // this works
102101
```
103102

104-
Now, let's mark Engine as PRIVATE.
103+
Now, let's mark `Engine` as private:
105104

106105
```
107106
class Car {
108107
constructor(@Host() e: Engine) {}
109108
}
110109
111-
var resolvedBindings = Injector.resolve([Car, Engine]);
112110
var parentProto = new ProtoInjector([
113-
new BindingWithVisibility(Engine, PRIVATE),
114-
new BindingWithVisibility(Car, PUBLIC)
111+
new BindingWithVisibility(Engine, Visibility.Private),
112+
new BindingWithVisibility(Car, Visibility.Public)
115113
]);
116114
var parent = new Injector(parentProto);
117115
118-
var hostChildProto = new ProtoInjector([new BindingWithVisibility(Car, PUBLIC)]);
116+
var hostChildProto = new ProtoInjector([new BindingWithVisibility(Car, Visibility.Public)]);
119117
var hostChild = new Injector(hostChildProto, parent, true);
120118
121-
var regularChildProto = new ProtoInjector([new BindingWithVisibility(Car, PUBLIC)]);
119+
var regularChildProto = new ProtoInjector([new BindingWithVisibility(Car, Visibility.Public)]);
122120
var regularChild = new Injector(regularChildProto, parent, false);
123121
124122
hostChild.get(Car); // this works
125123
parent.get(Car); // this throws
126124
regularChild.get(Car); // this throws
127125
```
128126

129-
Now, let's mark Engine as both PUBLIC and PRIVATE.
127+
Now, let's mark `Engine` as both public and private:
130128

131129
```
132130
class Car {
133131
constructor(@Host() e: Engine) {}
134132
}
135133
136-
var resolvedBindings = Injector.resolve([Car, Engine]);
137134
var parentProto = new ProtoInjector([
138-
new BindingWithVisibility(Engine, PUBLIC_AND_PRIVATE),
139-
new BindingWithVisibility(Car, PUBLIC)
135+
new BindingWithVisibility(Engine, Visibility.PublicAndPrivate),
136+
new BindingWithVisibility(Car, Visibility.Public)
140137
]);
141138
var parent = new Injector(parentProto);
142139
143-
var hostChildProto = new ProtoInjector([new BindingWithVisibility(Car, PUBLIC)]);
140+
var hostChildProto = new ProtoInjector([new BindingWithVisibility(Car, Visibility.Public)]);
144141
var hostChild = new Injector(hostChildProto, parent, true);
145142
146-
var regularChildProto = new ProtoInjector([new BindingWithVisibility(Car, PUBLIC)]);
143+
var regularChildProto = new ProtoInjector([new BindingWithVisibility(Car, Visibility.Public)]);
147144
var regularChild = new Injector(regularChildProto, parent, false);
148145
149146
hostChild.get(Car); // this works
@@ -168,21 +165,21 @@ Let's look at a complex example that shows how the injector tree gets created.
168165

169166
```
170167
<my-component my-directive>
171-
<needs-service></needs-service>
168+
<needs-service></needs-service>
172169
</my-component>
173170
```
174171

175-
Both MyComponent and MyDirective are created on the same element.
172+
Both `MyComponent` and `MyDirective` are created on the same element.
176173

177174
```
178175
@Component({
179-
selector: 'my-component,
180-
bindings: [
181-
bind("componentService").toValue("Host_MyComponentService")
182-
],
183-
viewBindings: [
184-
bind("viewService").toValue("View_MyComponentService")
185-
]
176+
selector: 'my-component',
177+
bindings: [
178+
bind('componentService').toValue('Host_MyComponentService')
179+
],
180+
viewBindings: [
181+
bind('viewService').toValue('View_MyComponentService')
182+
]
186183
})
187184
@View({
188185
template: `<needs-view-service></needs-view-service>`,
@@ -193,14 +190,14 @@ class MyComponent {}
193190
@Directive({
194191
selector: '[my-directive]',
195192
bindings: [
196-
bind("directiveService").toValue("MyDirectiveService")
193+
bind('directiveService').toValue('MyDirectiveService')
197194
]
198195
})
199196
class MyDirective {
200197
}
201198
```
202199

203-
NeedsService and NeedsViewService look like this:
200+
`NeedsService` and `NeedsViewService` look like this:
204201

205202
```
206203
@Directive({
@@ -222,18 +219,18 @@ class NeedsService {
222219
This will create the following injector tree.
223220

224221
```
225-
Injector1 [
226-
{binding: MyComponent, visibility: PUBLIC_AND_PRIVATE},
227-
{binding: "componentService", visibility: PUBLIC_AND_PRIVATE},
228-
{binding: "viewService", visibility: PRIVATE},
229-
{binding: MyDirective visibility: PUBLIC},
230-
{binding: "directiveService", visibility: PUBLIC}
231-
]
222+
Injector1 [
223+
{binding: MyComponent, visibility: Visibility.PublicAndPrivate},
224+
{binding: 'componentService', visibility: Visibility.PublicAndPrivate},
225+
{binding: 'viewService', visibility: Visibility.Private},
226+
{binding: MyDirective visibility: Visibility.Public},
227+
{binding: 'directiveService', visibility: Visibility.Public}
228+
]
232229
/ \
233230
| \ host
234-
Injector2 [ Injector3 [
235-
{binding: NeedsService, visibility: PUBLIC} {binding: NeedsViewService, visibility: PUBLIC}
236-
] ]
231+
Injector2 [ Injector3 [
232+
{binding: NeedsService, visibility: Visibility.Public} {binding: NeedsViewService, visibility: Visibility.Public}
233+
] ]
237234
```
238235

239236
As you can see the component and its bindings can be seen by its children and its view. The view bindings can be seen only by the view. And the bindings of other directives can be seen only their children.

0 commit comments

Comments
 (0)