You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/angular2/docs/core/02_directives.md
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Directives are the cornerstone of an Angular application. We use Directives to b
6
6
7
7
Angular applications do not have a main method. Instead they have a root Component. Dependency Injection then assembles the directives into a working Angular application.
8
8
9
-
There are three different kinds of directives (described in more detail in later sections).
9
+
There are three different kinds of directives (described in more detail in later sections).
10
10
11
11
1.*Decorators*: can be placed on any DOM element and can be combined with other directives.
12
12
2.*Components*: Components have an encapsulated view and can configure injectors.
@@ -16,7 +16,7 @@ There are three different kinds of directives (described in more detail in later
16
16
17
17
## CSS Selectors
18
18
19
-
Directives are instantiated whenever the CSS selector matches the DOM structure.
19
+
Directives are instantiated whenever the CSS selector matches the DOM structure.
20
20
21
21
Angular supports these CSS selector constructs:
22
22
* Element name: `name`
@@ -29,7 +29,7 @@ Angular supports these CSS selector constructs:
29
29
30
30
Angular does not support these (and any CSS selector which crosses element boundaries):
31
31
* Descendant: `body div`
32
-
* Direct descendant: `body > div`
32
+
* Direct descendant: `body > div`
33
33
* Adjacent: `div + table`
34
34
* Sibling: `div ~ table`
35
35
* Wildcard: `*`
@@ -68,10 +68,10 @@ Here is a trivial example of a tooltip decorator. The directive will log a toolt
68
68
@Decorator({
69
69
selector: '[tooltip]', // CSS Selector which triggers the decorator
70
70
properties: { // List which properties need to be bound
71
-
text: 'tooltip' // - DOM element tooltip property should be
71
+
text: 'tooltip' // - DOM element tooltip property should be
72
72
}, // mapped to the directive text property.
73
73
hostListeners: { // List which events need to be mapped.
74
-
mouseover: 'show' // - Invoke the show() method every time
74
+
mouseover: 'show' // - Invoke the show() method every time
75
75
} // the mouseover event is fired.
76
76
})
77
77
class Form { // Directive controller class, instantiated
@@ -121,14 +121,14 @@ Example of a component:
121
121
templateUrl: 'pane.html' | - URL of template HTML
122
122
}) |
123
123
class Pane { | Component controller class
124
-
title:string; | - title property
124
+
title:string; | - title property
125
125
open:boolean;
126
-
126
+
127
127
constructor() {
128
128
this.title = '';
129
129
this.open = true;
130
130
}
131
-
131
+
132
132
// Public API
133
133
toggle() => this.open = !this.open;
134
134
open() => this.open = true;
@@ -165,12 +165,12 @@ Example of usage:
165
165
166
166
## Viewport
167
167
168
-
Viewport is a directive which can control instantiation of child views which are then inserted into the DOM. (Examples are `if` and `for`.)
168
+
Viewport is a directive which can control instantiation of child views which are then inserted into the DOM. (Examples are `if` and `for`.)
169
169
170
170
* Viewports can only be placed on `<template>` elements (or the short hand version which uses `<element template>` attribute.)
171
171
* Only one viewport can be present per DOM template element.
172
-
* The viewport is created over the `template` element. This is known as the `ViewContainer`.
173
-
* Viewport can insert child views into the `ViewContainer`. The child views show up as siblings of the `Viewport` in the DOM.
172
+
* The viewport is created over the `template` element. This is known as the `ViewContainerRef`.
173
+
* Viewport can insert child views into the `ViewContainerRef`. The child views show up as siblings of the `Viewport` in the DOM.
174
174
175
175
>> TODO(misko): Relationship with Injection
176
176
>> TODO(misko): Instantiator can not be injected into child Views
@@ -184,10 +184,10 @@ Viewport is a directive which can control instantiation of child views which are
184
184
}
185
185
})
186
186
export class If {
187
-
viewContainer: ViewContainer;
187
+
viewContainer: ViewContainerRef;
188
188
view: View;
189
189
190
-
constructor(viewContainer: ViewContainer) {
190
+
constructor(viewContainer: ViewContainerRef) {
191
191
this.viewContainer = viewContainer;
192
192
this.view = null;
193
193
}
@@ -220,30 +220,30 @@ To better understand the kinds of injections which are supported in Angular we h
220
220
221
221
### Injecting Services
222
222
223
-
Service injection is the most straight forward kind of injection which Angular supports. It involves a component configuring the `injectables` and then letting the directive ask for the configured service.
223
+
Service injection is the most straight forward kind of injection which Angular supports. It involves a component configuring the `injectables` and then letting the directive ask for the configured service.
224
224
225
225
This example illustrates how to inject `MyService` into `House` directive.
226
226
227
227
228
228
```
229
-
class MyService {} | Assume a service which needs to be injected
229
+
class MyService {} | Assume a service which needs to be injected
230
230
| into a directive.
231
231
|
232
-
@Component({ | Assume a top level application component which
232
+
@Component({ | Assume a top level application component which
233
233
selector: 'my-app', | configures the services to be injected.
234
-
injectables: [MyService] |
234
+
injectables: [MyService] |
235
235
}) |
236
236
@View({ | Assume we have a template that needs to be
237
237
templateUrl: 'my_app.html', | configured with directives to be injected.
238
-
directives: [House] |
238
+
directives: [House] |
239
239
}) |
240
240
class MyApp {} |
241
241
|
242
-
@Decorator({ | This is the directive into which we would like
242
+
@Decorator({ | This is the directive into which we would like
243
243
selector: '[house]' | to inject the MyService.
244
244
}) |
245
245
class House { |
246
-
constructor(myService:MyService) { | Notice that in the constructor we can simply
246
+
constructor(myService:MyService) { | Notice that in the constructor we can simply
247
247
} | ask for MyService.
248
248
} |
249
249
@@ -252,7 +252,7 @@ class House { |
252
252
253
253
Assume the following DOM structure for `my_app.html`:
254
254
```
255
-
<div house> | The house attribute triggers the creation of the House directive.
255
+
<div house> | The house attribute triggers the creation of the House directive.
256
256
</div> | This is equivalent to:
257
257
| new House(injector.get(MyService));
258
258
```
@@ -264,7 +264,7 @@ Injecting other directives into directives follows a similar mechanism as inject
264
264
265
265
There are five kinds of visibilities:
266
266
267
-
* (no annotation): Inject dependant directives only if they are on the current element.
267
+
* (no annotation): Inject dependant directives only if they are on the current element.
268
268
*`@ancestor`: Inject a directive if it is at any element above the current element.
269
269
*`@parent`: Inject a directive which is direct parent of the current element.
270
270
*`@child`: Inject a list of direct children which match a given type. (Used with `Query`)
@@ -278,8 +278,8 @@ Here is an example of the kinds of injections which can be achieved:
278
278
```
279
279
@Component({ |
280
280
selector: 'my-app' |
281
-
}) |
282
-
@View({ |
281
+
}) |
282
+
@View({ |
283
283
templateUrl: 'my_app.html', |
284
284
directives: [Form, FieldSet, |
285
285
Field, Primary] |
@@ -290,28 +290,28 @@ class MyApp {} |
290
290
class Form { |
291
291
constructor( |
292
292
@descendant sets:Query<FieldSet> |
293
-
) { |
294
-
} |
293
+
) { |
294
+
} |
295
295
} |
296
296
|
297
297
@Decorator({ selector: 'fieldset' }) |
298
298
class FieldSet { |
299
299
constructor( |
300
300
@child sets:Query<Field> |
301
-
) { ... } |
301
+
) { ... } |
302
302
} |
303
303
|
304
304
@Decorator({ selector: 'field' }) |
305
305
class Field { |
306
306
constructor( |
307
307
@ancestor field:Form, |
308
308
@parent field:FieldSet, |
309
-
) { ... } |
309
+
) { ... } |
310
310
} |
311
311
|
312
312
@Decorator({ selector: '[primary]'}) |
313
313
class Primary { |
314
-
constructor(field:Field ) { ... } |
314
+
constructor(field:Field ) { ... } |
315
315
} |
316
316
```
317
317
@@ -324,7 +324,7 @@ Assume the following DOM structure for `my_app.html`:
0 commit comments